-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path225-implement-stack.py
More file actions
54 lines (36 loc) · 1023 Bytes
/
Copy path225-implement-stack.py
File metadata and controls
54 lines (36 loc) · 1023 Bytes
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
"""
problem_name = Implement Stack using Queues
problem_source = https://leetcode.com/problems/implement-stack-using-queues/
Algo
-
"""
from collections import deque
class MyStack:
def __init__(self):
self.list = list()
def push(self, x: int) -> None:
self.list.append(x)
def pop(self) -> int:
return self.list.pop()
def top(self) -> int:
return self.list[-1]
def empty(self) -> bool:
return False if self.list else True
# Another approach - using dequeue
class MyStack:
def __init__(self):
self.queue = deque()
def push(self, x: int) -> None:
self.queue.append(x)
def pop(self) -> int:
return self.queue.pop()
def top(self) -> int:
return self.queue[-1]
def empty(self) -> bool:
return False if self.queue else True
# 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()