-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path225.implement-stack-using-queues.py
67 lines (53 loc) · 1.55 KB
/
225.implement-stack-using-queues.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# https://leetcode.cn/problems/plus-one
class MyStack:
'''
Date: 2023.08.04
Pass/Error/Bug: 1/0/0
执行用时: 48 ms, 在所有 Python3 提交中击败了 22.37% 的用户
内存消耗:15.64 MB, 在所有 Python3 提交中击败了 62.97% 的用户
'''
def __init__(self):
self.stack = []
def push(self, x: int) -> None:
self.stack.append(x)
def pop(self) -> int:
x = self.stack[-1]
self.stack = self.stack[:-1]
return x
def top(self) -> int:
return self.stack[-1]
def empty(self) -> bool:
if len(self.stack) == 0:
return True
else:
return False
class MyStack:
'''
Date: 2023.08.05
Pass/Error/Bug: 1/0/0
执行用时: 48 ms, 在所有 Python3 提交中击败了 22.37% 的用户
内存消耗:15.54 MB, 在所有 Python3 提交中击败了 69.77% 的用户
'''
def __init__(self):
self.queue = []
def push(self, x: int) -> None:
self.queue.append(x)
total_n = len(self.queue)
for i in range(total_n-1):
self.queue.append(self.queue.pop(0))
def pop(self) -> int:
x = self.queue.pop(0)
return x
def top(self) -> int:
return self.queue[0]
def empty(self) -> bool:
if len(self.queue) == 0:
return True
else:
return False
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()