|
| 1 | +''' |
| 2 | +Implement the following operations of a stack using queues. |
| 3 | +
|
| 4 | +push(x) -- Push element x onto stack. |
| 5 | +pop() -- Removes the element on top of the stack. |
| 6 | +top() -- Get the top element. |
| 7 | +empty() -- Return whether the stack is empty. |
| 8 | +Example: |
| 9 | +
|
| 10 | +MyStack stack = new MyStack(); |
| 11 | +
|
| 12 | +stack.push(1); |
| 13 | +stack.push(2); |
| 14 | +stack.top(); // returns 2 |
| 15 | +stack.pop(); // returns 2 |
| 16 | +stack.empty(); // returns false |
| 17 | +Notes: |
| 18 | +
|
| 19 | +You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid. |
| 20 | +Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. |
| 21 | +You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). |
| 22 | +
|
| 23 | +''' |
| 24 | + |
| 25 | + |
| 26 | +class MyStack: |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + """ |
| 30 | + Initialize your data structure here. |
| 31 | + """ |
| 32 | + self.q1 = [] |
| 33 | + self.q2 = [] |
| 34 | + |
| 35 | + |
| 36 | + def push(self, x: int) -> None: |
| 37 | + """ |
| 38 | + Push element x onto stack. |
| 39 | + """ |
| 40 | + if self.q1: self.q1.append(x) |
| 41 | + else: self.q2.append(x) |
| 42 | + |
| 43 | + def pop(self) -> int: |
| 44 | + """ |
| 45 | + Removes the element on top of the stack and returns that element. |
| 46 | + """ |
| 47 | + if self.q1: |
| 48 | + while len(self.q1) > 1: |
| 49 | + self.q2.append(self.q1.pop(0)) |
| 50 | + return self.q1.pop(0) |
| 51 | + else: |
| 52 | + while len(self.q2) > 1: |
| 53 | + self.q1.append(self.q2.pop(0)) |
| 54 | + return self.q2.pop(0) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + def top(self) -> int: |
| 59 | + """ |
| 60 | + Get the top element. |
| 61 | + """ |
| 62 | + if self.q1: |
| 63 | + while len(self.q1) > 1: |
| 64 | + self.q2.append(self.q1.pop(0)) |
| 65 | + key = self.q1.pop(0) |
| 66 | + self.q2.append(key) |
| 67 | + return key |
| 68 | + else: |
| 69 | + while len(self.q2) > 1: |
| 70 | + self.q1.append(self.q2.pop(0)) |
| 71 | + key = self.q2.pop(0) |
| 72 | + self.q1.append(key) |
| 73 | + return key |
| 74 | + |
| 75 | + def empty(self) -> bool: |
| 76 | + """ |
| 77 | + Returns whether the stack is empty. |
| 78 | + """ |
| 79 | + return self.q1 == [] and self.q2 == [] |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# Your MyStack object will be instantiated and called as such: |
| 84 | +# obj = MyStack() |
| 85 | +# obj.push(x) |
| 86 | +# param_2 = obj.pop() |
| 87 | +# param_3 = obj.top() |
| 88 | +# param_4 = obj.empty() |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +# Your MyStack object will be instantiated and called as such: |
| 93 | +# obj = MyStack() |
| 94 | +# obj.push(x) |
| 95 | +# param_2 = obj.pop() |
| 96 | +# param_3 = obj.top() |
| 97 | +# param_4 = obj.empty() |
0 commit comments