|
1 |
| -# [03.04. Implement Queue using Stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>Implement a MyQueue class which implements a queue using two stacks.</p> |
5 |
| - |
6 |
| -
|
7 |
| -<p><strong>Example: </strong></p> |
8 |
| -
|
9 |
| -<pre> |
10 |
| -MyQueue queue = new MyQueue(); |
11 |
| -
|
12 |
| -queue.push(1); |
13 |
| -queue.push(2); |
14 |
| -queue.peek(); // return 1 |
15 |
| -queue.pop(); // return 1 |
16 |
| -queue.empty(); // return false</pre> |
17 |
| -
|
18 |
| -<p> </p> |
19 |
| -
|
20 |
| -<p><b>Notes:</b></p> |
21 |
| -
|
22 |
| -<ul> |
23 |
| - <li>You must use <i>only</i> standard operations of a stack -- which means only <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code>, and <code>is empty</code> operations are valid.</li> |
24 |
| - <li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li> |
25 |
| - <li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li> |
26 |
| -</ul> |
27 |
| -
|
28 |
| -<p> </p> |
29 |
| - |
30 |
| - |
31 |
| - |
32 |
| -## Solutions |
33 |
| - |
34 |
| - |
35 |
| -### Python3 |
36 |
| - |
37 |
| -```python |
38 |
| - |
39 |
| -``` |
40 |
| - |
41 |
| -### Java |
42 |
| - |
43 |
| -```java |
44 |
| - |
45 |
| -``` |
46 |
| - |
47 |
| -### ... |
48 |
| -``` |
49 |
| - |
50 |
| -``` |
| 1 | +# [03.04. Implement Queue using Stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>Implement a MyQueue class which implements a queue using two stacks.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +<p><strong>Example: </strong></p> |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +<pre> |
| 15 | + |
| 16 | +MyQueue queue = new MyQueue(); |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +queue.push(1); |
| 21 | + |
| 22 | +queue.push(2); |
| 23 | + |
| 24 | +queue.peek(); // return 1 |
| 25 | + |
| 26 | +queue.pop(); // return 1 |
| 27 | + |
| 28 | +queue.empty(); // return false</pre> |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +<p> </p> |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +<p><b>Notes:</b></p> |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +<ul> |
| 41 | + |
| 42 | + <li>You must use <i>only</i> standard operations of a stack -- which means only <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code>, and <code>is empty</code> operations are valid.</li> |
| 43 | + |
| 44 | + <li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li> |
| 45 | + |
| 46 | + <li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li> |
| 47 | + |
| 48 | +</ul> |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +<p> </p> |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Solutions |
| 58 | + |
| 59 | + |
| 60 | +### Python3 |
| 61 | + |
| 62 | +```python |
| 63 | +class MyQueue: |
| 64 | + |
| 65 | + def __init__(self): |
| 66 | + """ |
| 67 | + Initialize your data structure here. |
| 68 | + """ |
| 69 | + self._s1, self._s2 = [], [] |
| 70 | + |
| 71 | + |
| 72 | + def push(self, x: int) -> None: |
| 73 | + """ |
| 74 | + Push element x to the back of queue. |
| 75 | + """ |
| 76 | + self._s1.append(x) |
| 77 | + |
| 78 | + |
| 79 | + def pop(self) -> int: |
| 80 | + """ |
| 81 | + Removes the element from in front of queue and returns that element. |
| 82 | + """ |
| 83 | + if len(self._s2) == 0: |
| 84 | + while self._s1: |
| 85 | + self._s2.append(self._s1.pop()) |
| 86 | + return self._s2.pop() |
| 87 | + |
| 88 | + |
| 89 | + def peek(self) -> int: |
| 90 | + """ |
| 91 | + Get the front element. |
| 92 | + """ |
| 93 | + if len(self._s2) == 0: |
| 94 | + while self._s1: |
| 95 | + self._s2.append(self._s1.pop()) |
| 96 | + return self._s2[-1] |
| 97 | + |
| 98 | + |
| 99 | + def empty(self) -> bool: |
| 100 | + """ |
| 101 | + Returns whether the queue is empty. |
| 102 | + """ |
| 103 | + return len(self._s1) + len(self._s2) == 0 |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +# Your MyQueue object will be instantiated and called as such: |
| 108 | +# obj = MyQueue() |
| 109 | +# obj.push(x) |
| 110 | +# param_2 = obj.pop() |
| 111 | +# param_3 = obj.peek() |
| 112 | +# param_4 = obj.empty() |
| 113 | +``` |
| 114 | + |
| 115 | +### Java |
| 116 | + |
| 117 | +```java |
| 118 | +class MyQueue { |
| 119 | + private Stack<Integer> s1; |
| 120 | + private Stack<Integer> s2; |
| 121 | + |
| 122 | + /** Initialize your data structure here. */ |
| 123 | + public MyQueue() { |
| 124 | + s1 = new Stack<>(); |
| 125 | + s2 = new Stack<>(); |
| 126 | + } |
| 127 | + |
| 128 | + /** Push element x to the back of queue. */ |
| 129 | + public void push(int x) { |
| 130 | + s1.push(x); |
| 131 | + } |
| 132 | + |
| 133 | + /** Removes the element from in front of queue and returns that element. */ |
| 134 | + public int pop() { |
| 135 | + if (s2.empty()) { |
| 136 | + while (!s1.empty()) { |
| 137 | + s2.push(s1.pop()); |
| 138 | + } |
| 139 | + } |
| 140 | + return s2.pop(); |
| 141 | + } |
| 142 | + |
| 143 | + /** Get the front element. */ |
| 144 | + public int peek() { |
| 145 | + if (s2.empty()) { |
| 146 | + while (!s1.empty()) { |
| 147 | + s2.push(s1.pop()); |
| 148 | + } |
| 149 | + } |
| 150 | + return s2.peek(); |
| 151 | + } |
| 152 | + |
| 153 | + /** Returns whether the queue is empty. */ |
| 154 | + public boolean empty() { |
| 155 | + return s1.empty() && s2.empty(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Your MyQueue object will be instantiated and called as such: |
| 161 | + * MyQueue obj = new MyQueue(); |
| 162 | + * obj.push(x); |
| 163 | + * int param_2 = obj.pop(); |
| 164 | + * int param_3 = obj.peek(); |
| 165 | + * boolean param_4 = obj.empty(); |
| 166 | + */ |
| 167 | +``` |
| 168 | + |
| 169 | +### ... |
| 170 | +``` |
| 171 | +
|
| 172 | +``` |
0 commit comments