We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 64c1491 commit a3f4d8aCopy full SHA for a3f4d8a
0225-implement-stack-using-queues/0225-implement-stack-using-queues.py
@@ -0,0 +1,34 @@
1
+from queue import SimpleQueue
2
+
3
+class MyStack:
4
+ def __init__(self):
5
+ self.q = SimpleQueue()
6
+ return
7
8
+ def push(self, x: int) -> None:
9
+ self.q.put(x)
10
11
12
+ def pop(self) -> int:
13
+ new_q = SimpleQueue()
14
+ while self.q.qsize() > 1:
15
+ new_q.put(self.q.get())
16
+ answer = self.q.get()
17
+ self.q = new_q
18
+ return answer
19
20
+ def top(self) -> int:
21
+ item = self.pop()
22
+ self.push(item)
23
+ return item
24
25
+ def empty(self) -> bool:
26
+ return self.q.empty()
27
28
29
+# Your MyStack object will be instantiated and called as such:
30
+# obj = MyStack()
31
+# obj.push(x)
32
+# param_2 = obj.pop()
33
+# param_3 = obj.top()
34
+# param_4 = obj.empty()
0 commit comments