Skip to content

Commit 227962d

Browse files
committed
Update 225.implement-stack-using-queues.py
1 parent 4e2973f commit 227962d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

225.implement-stack-using-queues.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ def empty(self) -> bool:
2929
return False
3030

3131

32+
class MyStack:
33+
'''
34+
Date: 2023.08.05
35+
Pass/Error/Bug: 1/0/0
36+
执行用时: 48 ms, 在所有 Python3 提交中击败了 22.37% 的用户
37+
内存消耗:15.54 MB, 在所有 Python3 提交中击败了 69.77% 的用户
38+
'''
39+
def __init__(self):
40+
self.queue = []
41+
42+
def push(self, x: int) -> None:
43+
self.queue.append(x)
44+
total_n = len(self.queue)
45+
for i in range(total_n-1):
46+
self.queue.append(self.queue.pop(0))
47+
48+
def pop(self) -> int:
49+
x = self.queue.pop(0)
50+
return x
51+
52+
def top(self) -> int:
53+
return self.queue[0]
54+
55+
def empty(self) -> bool:
56+
if len(self.queue) == 0:
57+
return True
58+
else:
59+
return False
60+
3261

3362
# Your MyStack object will be instantiated and called as such:
3463
# obj = MyStack()

0 commit comments

Comments
 (0)