mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
简化 0225.用队列实现栈.md python部分,冗余部分太多
原方法用了很多不必要的变量和操作,改进后的代码更直观,效率和空间都得到了优化
This commit is contained in:
@ -294,53 +294,66 @@ Python:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
||||||
class MyStack:
|
class MyStack:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Initialize your data structure here.
|
Python普通的Queue或SimpleQueue没有类似于peek的功能
|
||||||
|
也无法用索引访问,在实现top的时候较为困难。
|
||||||
|
|
||||||
|
用list可以,但是在使用pop(0)的时候时间复杂度为O(1)
|
||||||
|
因此这里使用双向队列,我们保证只执行popleft()和append(),因为deque可以用索引访问,可以实现和peek相似的功能
|
||||||
|
|
||||||
|
in - 存所有数据
|
||||||
|
out - 仅在pop的时候会用到
|
||||||
"""
|
"""
|
||||||
#使用两个队列来实现
|
self.queue_in = deque()
|
||||||
self.que1 = deque()
|
self.queue_out = deque()
|
||||||
self.que2 = deque()
|
|
||||||
|
|
||||||
def push(self, x: int) -> None:
|
def push(self, x: int) -> None:
|
||||||
"""
|
"""
|
||||||
Push element x onto stack.
|
直接append即可
|
||||||
"""
|
"""
|
||||||
self.que1.append(x)
|
self.queue_in.append(x)
|
||||||
|
|
||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""
|
"""
|
||||||
Removes the element on top of the stack and returns that element.
|
1. 首先确认不空
|
||||||
|
2. 因为队列的特殊性,FIFO,所以我们只有在pop()的时候才会使用queue_out
|
||||||
|
3. 先把queue_in中的所有元素(除了最后一个),依次出列放进queue_out
|
||||||
|
4. 交换in和out,此时out里只有一个元素
|
||||||
|
5. 把out中的pop出来,即是原队列的最后一个
|
||||||
|
|
||||||
|
tip:这不能像栈实现队列一样,因为另一个queue也是FIFO,如果执行pop()它不能像
|
||||||
|
stack一样从另一个pop(),所以干脆in只用来存数据,pop()的时候两个进行交换
|
||||||
"""
|
"""
|
||||||
size = len(self.que1)
|
if self.empty():
|
||||||
size -= 1#这里先减一是为了保证最后面的元素
|
return None
|
||||||
while size > 0:
|
|
||||||
size -= 1
|
|
||||||
self.que2.append(self.que1.popleft())
|
|
||||||
|
|
||||||
|
for i in range(len(self.queue_in) - 1):
|
||||||
result = self.que1.popleft()
|
self.queue_out.append(self.queue_in.popleft())
|
||||||
self.que1, self.que2= self.que2, self.que1#将que2和que1交换 que1经过之前的操作应该是空了
|
|
||||||
#一定注意不能直接使用que1 = que2 这样que2的改变会影响que1 可以用浅拷贝
|
self.queue_in, self.queue_out = self.queue_out, self.queue_in # 交换in和out,这也是为啥in只用来存
|
||||||
return result
|
return self.queue_out.popleft()
|
||||||
|
|
||||||
def top(self) -> int:
|
def top(self) -> int:
|
||||||
"""
|
"""
|
||||||
Get the top element.
|
1. 首先确认不空
|
||||||
|
2. 我们仅有in会存放数据,所以返回第一个即可
|
||||||
"""
|
"""
|
||||||
return self.que1[-1]
|
if self.empty():
|
||||||
|
return None
|
||||||
|
|
||||||
|
return self.queue_in[-1]
|
||||||
|
|
||||||
|
|
||||||
def empty(self) -> bool:
|
def empty(self) -> bool:
|
||||||
"""
|
"""
|
||||||
Returns whether the stack is empty.
|
因为只有in存了数据,只要判断in是不是有数即可
|
||||||
"""
|
"""
|
||||||
#print(self.que1)
|
return len(self.queue_in) == 0
|
||||||
if len(self.que1) == 0:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user