@@ -69,6 +69,18 @@ myQueue.empty(); // return false
69
69
70
70
<!-- 这里可写通用的实现逻辑 -->
71
71
72
+ ** 方法一:双栈**
73
+
74
+ 使用两个栈,其中栈 ` stk1 ` 用于入队,另一个栈 ` stk2 ` 用于出队。
75
+
76
+ 入队时,直接将元素入栈 ` stk1 ` 。时间复杂度 $O(1)$。
77
+
78
+ 出队时,先判断栈 ` stk2 ` 是否为空,如果为空,则将栈 ` stk1 ` 中的元素全部出栈并入栈 ` stk2 ` ,然后再从栈 ` stk2 ` 中出栈一个元素。如果栈 ` stk2 ` 不为空,则直接从栈 ` stk2 ` 中出栈一个元素。均摊时间复杂度 $O(1)$。
79
+
80
+ 获取队首元素时,先判断栈 ` stk2 ` 是否为空,如果为空,则将栈 ` stk1 ` 中的元素全部出栈并入栈 ` stk2 ` ,然后再从栈 ` stk2 ` 中获取栈顶元素。如果栈 ` stk2 ` 不为空,则直接从栈 ` stk2 ` 中获取栈顶元素。均摊时间复杂度 $O(1)$。
81
+
82
+ 判断队列是否为空时,只要判断两个栈是否都为空即可。时间复杂度 $O(1)$。
83
+
72
84
<!-- tabs:start -->
73
85
74
86
### ** Python3**
@@ -78,45 +90,27 @@ myQueue.empty(); // return false
78
90
``` python
79
91
class MyQueue :
80
92
def __init__ (self ):
81
- """
82
- Initialize your data structure here.
83
- """
84
- self .s1 = []
85
- self .s2 = []
93
+ self .stk1 = []
94
+ self .stk2 = []
86
95
87
96
def push (self , x : int ) -> None :
88
- """
89
- Push element x to the back of queue.
90
- """
91
- self .s1.append(x)
97
+ self .stk1.append(x)
92
98
93
99
def pop (self ) -> int :
94
- """
95
- Removes the element from in front of queue and returns that element.
96
- """
97
- self ._move()
98
- return self .s2.pop()
100
+ self .move()
101
+ return self .stk2.pop()
99
102
100
103
def peek (self ) -> int :
101
- """
102
- Get the front element.
103
- """
104
- self ._move()
105
- return self .s2[- 1 ]
104
+ self .move()
105
+ return self .stk2[- 1 ]
106
106
107
107
def empty (self ) -> bool :
108
- """
109
- Returns whether the queue is empty.
110
- """
111
- return len (self .s1) + len (self .s2) == 0
108
+ return not self .stk1 and not self .stk2
112
109
113
- def _move (self ):
114
- """
115
- Move elements from s1 to s2.
116
- """
117
- if len (self .s2) == 0 :
118
- while len (self .s1) > 0 :
119
- self .s2.append(self .s1.pop())
110
+ def move (self ):
111
+ if not self .stk2:
112
+ while self .stk1:
113
+ self .stk2.append(self .stk1.pop())
120
114
121
115
122
116
# Your MyQueue object will be instantiated and called as such:
@@ -133,41 +127,35 @@ class MyQueue:
133
127
134
128
``` java
135
129
class MyQueue {
130
+ private Deque<Integer > stk1 = new ArrayDeque<> ();
131
+ private Deque<Integer > stk2 = new ArrayDeque<> ();
136
132
137
- private Deque<Integer > s1 = new ArrayDeque<> ();
138
- private Deque<Integer > s2 = new ArrayDeque<> ();
139
-
140
- /* * Initialize your data structure here. */
141
133
public MyQueue () {
134
+
142
135
}
143
136
144
- /* * Push element x to the back of queue. */
145
137
public void push (int x ) {
146
- s1 . push(x);
138
+ stk1 . push(x);
147
139
}
148
140
149
- /* * Removes the element from in front of queue and returns that element. */
150
141
public int pop () {
151
142
move();
152
- return s2 . pop();
143
+ return stk2 . pop();
153
144
}
154
145
155
- /* * Get the front element. */
156
146
public int peek () {
157
147
move();
158
- return s2 . peek();
148
+ return stk2 . peek();
159
149
}
160
150
161
- /* * Returns whether the queue is empty. */
162
151
public boolean empty () {
163
- return s1 . isEmpty() && s2 . isEmpty();
152
+ return stk1 . isEmpty() && stk2 . isEmpty();
164
153
}
165
154
166
- /* * Move elements from s1 to s2. */
167
155
private void move () {
168
- if (s2 . isEmpty()) {
169
- while (! s1 . isEmpty()) {
170
- s2 . push(s1 . pop());
156
+ while (stk2 . isEmpty()) {
157
+ while (! stk1 . isEmpty()) {
158
+ stk2 . push(stk1 . pop());
171
159
}
172
160
}
173
161
}
@@ -183,41 +171,145 @@ class MyQueue {
183
171
*/
184
172
```
185
173
174
+ ### ** C++**
175
+
176
+ ``` cpp
177
+ class MyQueue {
178
+ public:
179
+ MyQueue() {
180
+ }
181
+
182
+ void push(int x) {
183
+ stk1.push(x);
184
+ }
185
+
186
+ int pop () {
187
+ move ();
188
+ int ans = stk2.top();
189
+ stk2.pop();
190
+ return ans;
191
+ }
192
+
193
+ int peek() {
194
+ move ();
195
+ return stk2.top();
196
+ }
197
+
198
+ bool empty() {
199
+ return stk1.empty() && stk2.empty();
200
+ }
201
+
202
+ private:
203
+ stack<int > stk1;
204
+ stack<int > stk2;
205
+
206
+ void move() {
207
+ if (stk2.empty()) {
208
+ while (!stk1.empty()) {
209
+ stk2.push(stk1.top());
210
+ stk1.pop();
211
+ }
212
+ }
213
+ }
214
+ };
215
+
216
+ /* *
217
+ * Your MyQueue object will be instantiated and called as such:
218
+ * MyQueue* obj = new MyQueue();
219
+ * obj->push(x);
220
+ * int param_2 = obj->pop();
221
+ * int param_3 = obj->peek();
222
+ * bool param_4 = obj->empty();
223
+ */
224
+ ```
225
+
226
+ ### ** Go**
227
+
228
+ ``` go
229
+ type MyQueue struct {
230
+ stk1 []int
231
+ stk2 []int
232
+ }
233
+
234
+ func Constructor () MyQueue {
235
+ return MyQueue{[]int {}, []int {}}
236
+ }
237
+
238
+ func (this *MyQueue ) Push (x int ) {
239
+ this.stk1 = append (this.stk1 , x)
240
+ }
241
+
242
+ func (this *MyQueue ) Pop () int {
243
+ this.move ()
244
+ ans := this.stk2 [len (this.stk2 )-1 ]
245
+ this.stk2 = this.stk2 [:len (this.stk2 )-1 ]
246
+ return ans
247
+ }
248
+
249
+ func (this *MyQueue ) Peek () int {
250
+ this.move ()
251
+ return this.stk2 [len (this.stk2 )-1 ]
252
+ }
253
+
254
+ func (this *MyQueue ) Empty () bool {
255
+ return len (this.stk1 ) == 0 && len (this.stk2 ) == 0
256
+ }
257
+
258
+ func (this *MyQueue ) move () {
259
+ if len (this.stk2 ) == 0 {
260
+ for len (this.stk1 ) > 0 {
261
+ this.stk2 = append (this.stk2 , this.stk1 [len (this.stk1 )-1 ])
262
+ this.stk1 = this.stk1 [:len (this.stk1 )-1 ]
263
+ }
264
+ }
265
+ }
266
+
267
+ /* *
268
+ * Your MyQueue object will be instantiated and called as such:
269
+ * obj := Constructor();
270
+ * obj.Push(x);
271
+ * param_2 := obj.Pop();
272
+ * param_3 := obj.Peek();
273
+ * param_4 := obj.Empty();
274
+ */
275
+ ```
276
+
186
277
### ** TypeScript**
187
278
188
279
``` ts
189
280
class MyQueue {
190
- stack1: number [];
191
- stack2: number [];
281
+ stk1: number [];
282
+ stk2: number [];
283
+
192
284
constructor () {
193
- this .stack1 = [];
194
- this .stack2 = [];
285
+ this .stk1 = [];
286
+ this .stk2 = [];
195
287
}
196
288
197
289
push(x : number ): void {
198
- this .stack1 .push (x );
290
+ this .stk1 .push (x );
199
291
}
200
292
201
293
pop(): number {
202
- if (! this .stack2 .length ) {
203
- while (this .stack1 .length ) {
204
- this .stack2 .push (this .stack1 .pop ());
205
- }
206
- }
207
- return this .stack2 .pop ();
294
+ this .move ();
295
+ return this .stk2 .pop ();
208
296
}
209
297
210
298
peek(): number {
211
- if (! this .stack2 .length ) {
212
- while (this .stack1 .length ) {
213
- this .stack2 .push (this .stack1 .pop ());
214
- }
215
- }
216
- return this .stack2 [this .stack2 .length - 1 ];
299
+ this .move ();
300
+ return this .stk2 [this .stk2 .length - 1 ];
217
301
}
218
302
219
303
empty(): boolean {
220
- return ! this .stack1 .length && ! this .stack2 .length ;
304
+ return ! this .stk1 .length && ! this .stk2 .length ;
305
+ }
306
+
307
+ move(): void {
308
+ if (! this .stk2 .length ) {
309
+ while (this .stk1 .length ) {
310
+ this .stk2 .push (this .stk1 .pop ());
311
+ }
312
+ }
221
313
}
222
314
}
223
315
0 commit comments