31
31
32
32
## 解法
33
33
34
- - 两个栈,一个负责** 输入** ,一个负责** 输出** ;
35
- - 执行输入时,只放入输入栈中;
36
- - 执行输出时,将输入栈的所有元素依次出栈,放入输出栈中;
37
- - 根据栈的特点,处于输入栈** 栈底** 的元素,在输出栈中便是** 栈顶** ;
38
- - 只有输出栈中没有元素时才进行倒放,而非每一次。
34
+ ** 方法一:双栈**
35
+
36
+ 我们可以使用两个栈来实现队列,其中一个栈 ` stk1 ` 用来存储输入的元素,另一个栈 ` stk2 ` 用来输出元素。
37
+
38
+ 当调用 ` appendTail() ` 方法时,我们将元素压入 ` stk1 ` 中。
39
+
40
+ 当调用 ` deleteHead() ` 方法时,如果此时栈 ` stk2 ` 为空,我们将栈 ` stk1 ` 中的元素逐个弹出并压入栈 ` stk2 ` 中,然后弹出栈 ` stk2 ` 的栈顶元素即可。如果此时栈 ` stk2 ` 不为空,我们直接弹出栈 ` stk2 ` 的栈顶元素即可。如果两个栈都为空,说明队列中没有元素,返回 ` -1 ` 。
41
+
42
+ 时间复杂度上,对于 ` appendTail() ` 方法,时间复杂度为 $O(1)$;对于 ` deleteHead() ` 方法,时间复杂度为 $O(n)$;空间复杂度为 $O(n)$。其中 $n$ 为队列中的元素个数。
39
43
40
44
<!-- tabs:start -->
41
45
@@ -95,52 +99,55 @@ class CQueue {
95
99
*/
96
100
```
97
101
98
- ### ** JavaScript **
102
+ ### ** C++ **
99
103
100
- ``` js
101
- var CQueue = function () {
102
- this .stk1 = [];
103
- this .stk2 = [];
104
- };
104
+ ``` cpp
105
+ class CQueue {
106
+ public:
107
+ CQueue() {
105
108
106
- /**
107
- * @param {number} value
108
- * @return {void}
109
- */
110
- CQueue .prototype .appendTail = function (value ) {
111
- this .stk1 .push (value);
112
- };
109
+ }
113
110
114
- /**
115
- * @return {number}
116
- */
117
- CQueue .prototype .deleteHead = function () {
118
- if (! this .stk2 .length ) {
119
- while (this .stk1 .length ) {
120
- this .stk2 .push (this .stk1 .pop ());
111
+ void appendTail (int value) {
112
+ stk1.push(value);
113
+ }
114
+
115
+ int deleteHead() {
116
+ if (stk2.empty()) {
117
+ while (!stk1.empty()) {
118
+ stk2.push(stk1.top());
119
+ stk1.pop();
120
+ }
121
+ }
122
+ if (stk2.empty()) {
123
+ return -1;
121
124
}
125
+ int ans = stk2.top();
126
+ stk2.pop();
127
+ return ans;
122
128
}
123
- return this .stk2 .length ? this .stk2 .pop () : - 1 ;
129
+
130
+ private:
131
+ stack<int > stk1, stk2;
124
132
};
125
133
126
134
/**
127
135
* Your CQueue object will be instantiated and called as such:
128
- * var obj = new CQueue()
129
- * obj. appendTail(value)
130
- * var param_2 = obj. deleteHead()
136
+ * CQueue * obj = new CQueue();
137
+ * obj-> appendTail(value);
138
+ * int param_2 = obj-> deleteHead();
131
139
* /
132
140
```
133
141
134
142
### **Go**
135
143
136
144
```go
137
145
type CQueue struct {
138
- stk1 []int
139
- stk2 []int
146
+ stk1, stk2 []int
140
147
}
141
148
142
149
func Constructor() CQueue {
143
- return CQueue{stk1: []int {}, stk2: []int {}}
150
+ return CQueue{[]int{}, []int{}}
144
151
}
145
152
146
153
func (this *CQueue) AppendTail(value int) {
@@ -151,14 +158,14 @@ func (this *CQueue) DeleteHead() int {
151
158
if len(this.stk2) == 0 {
152
159
for len(this.stk1) > 0 {
153
160
this.stk2 = append(this.stk2, this.stk1[len(this.stk1)-1])
154
- this.stk1 = this.stk1 [0 : len (this.stk1 )-1 ]
161
+ this.stk1 = this.stk1[: len(this.stk1)-1]
155
162
}
156
163
}
157
164
if len(this.stk2) == 0 {
158
165
return -1
159
166
}
160
167
ans := this.stk2[len(this.stk2)-1]
161
- this.stk2 = this.stk2 [0 : len (this.stk2 )-1 ]
168
+ this.stk2 = this.stk2[: len(this.stk2)-1]
162
169
return ans
163
170
}
164
171
@@ -170,64 +177,65 @@ func (this *CQueue) DeleteHead() int {
170
177
*/
171
178
```
172
179
173
- ### ** C++**
174
-
175
- ``` cpp
176
- class CQueue {
177
- private:
178
- stack<int > s1, s2;
180
+ ### ** JavaScript**
179
181
180
- public:
181
- CQueue() {
182
- }
182
+ ``` js
183
+ var CQueue = function () {
184
+ this .stk1 = [];
185
+ this .stk2 = [];
186
+ };
183
187
184
- void appendTail(int value) {
185
- s1.push(value);
186
- }
188
+ /**
189
+ * @param {number} value
190
+ * @return {void}
191
+ */
192
+ CQueue .prototype .appendTail = function (value ) {
193
+ this .stk1 .push (value);
194
+ };
187
195
188
- int deleteHead () {
189
- if (s2.empty()) {
190
- while (!s1.empty()) {
191
- s2.push(s1.top());
192
- s1.pop();
193
- }
194
- }
195
- if (s2.empty()) {
196
- return -1;
196
+ /**
197
+ * @return {number}
198
+ */
199
+ CQueue .prototype .deleteHead = function () {
200
+ if (! this .stk2 .length ) {
201
+ while (this .stk1 .length ) {
202
+ this .stk2 .push (this .stk1 .pop ());
197
203
}
198
- int head = s2.top();
199
- s2.pop();
200
- return head;
201
204
}
205
+ return this .stk2 .length ? this .stk2 .pop () : - 1 ;
202
206
};
207
+
208
+ /**
209
+ * Your CQueue object will be instantiated and called as such:
210
+ * var obj = new CQueue()
211
+ * obj.appendTail(value)
212
+ * var param_2 = obj.deleteHead()
213
+ */
203
214
```
204
215
205
216
### ** TypeScript**
206
217
207
218
``` ts
208
219
class CQueue {
209
- private stack1: number [];
210
- private stack2: number [];
220
+ private stk1: number [];
221
+ private stk2: number [];
222
+
211
223
constructor () {
212
- this .stack1 = [];
213
- this .stack2 = [];
224
+ this .stk1 = [];
225
+ this .stk2 = [];
214
226
}
215
227
216
228
appendTail(value : number ): void {
217
- this .stack1 .push (value );
218
- }
219
-
220
- move(): void {
221
- while (this .stack1 .length != 0 ) {
222
- this .stack2 .push (this .stack1 .pop ());
223
- }
229
+ this .stk1 .push (value );
224
230
}
225
231
226
232
deleteHead(): number {
227
- if (this .stack2 .length == 0 ) {
228
- this .move ();
233
+ if (this .stk2 .length == 0 ) {
234
+ while (this .stk1 .length ) {
235
+ this .stk2 .push (this .stk1 .pop ());
236
+ }
229
237
}
230
- return this .stack2 .length == 0 ? - 1 : this .stack2 .pop ();
238
+ return this .stk2 .length == 0 ? - 1 : this .stk2 .pop ();
231
239
}
232
240
}
233
241
@@ -290,25 +298,24 @@ impl CQueue {
290
298
291
299
``` cs
292
300
public class CQueue {
293
- private Stack <int > stack1 ;
294
- private Stack <int > stack2 ;
301
+ private Stack <int > stk1 = new Stack < int >() ;
302
+ private Stack <int > stk2 = new Stack < int >() ;
295
303
296
304
public CQueue () {
297
- stack1 = new Stack <int >();
298
- stack2 = new Stack <int >();
305
+
299
306
}
300
307
301
308
public void AppendTail (int value ) {
302
- stack1 .Push (value );
309
+ stk1 .Push (value );
303
310
}
304
311
305
312
public int DeleteHead () {
306
- if (stack2 .Count == 0 ) {
307
- while (stack1 .Count != 0 ) {
308
- stack2 .Push (stack1 .Pop ());
313
+ if (stk2 .Count == 0 ) {
314
+ while (stk1 .Count != 0 ) {
315
+ stk2 .Push (stk1 .Pop ());
309
316
}
310
317
}
311
- return stack2 .Count == 0 ? - 1 : stack2 .Pop ();
318
+ return stk2 .Count == 0 ? - 1 : stk2 .Pop ();
312
319
}
313
320
}
314
321
0 commit comments