File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed
solution/0200-0299/0232.Implement Queue using Stacks Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,54 @@ class MyQueue {
192
192
*/
193
193
```
194
194
195
+ ### ** TypeScript**
196
+
197
+ ``` ts
198
+ class MyQueue {
199
+ stack1: number [];
200
+ stack2: number [];
201
+ constructor () {
202
+ this .stack1 = [];
203
+ this .stack2 = [];
204
+ }
205
+
206
+ push(x : number ): void {
207
+ this .stack1 .push (x );
208
+ }
209
+
210
+ pop(): number {
211
+ if (! this .stack2 .length ) {
212
+ while (this .stack1 .length ) {
213
+ this .stack2 .push (this .stack1 .pop ());
214
+ }
215
+ }
216
+ return this .stack2 .pop ();
217
+ }
218
+
219
+ peek(): number {
220
+ if (! this .stack2 .length ) {
221
+ while (this .stack1 .length ) {
222
+ this .stack2 .push (this .stack1 .pop ());
223
+ }
224
+ }
225
+ return this .stack2 [this .stack2 .length - 1 ];
226
+ }
227
+
228
+ empty(): boolean {
229
+ return ! this .stack1 .length && ! this .stack2 .length ;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Your MyQueue object will be instantiated and called as such:
235
+ * var obj = new MyQueue()
236
+ * obj.push(x)
237
+ * var param_2 = obj.pop()
238
+ * var param_3 = obj.peek()
239
+ * var param_4 = obj.empty()
240
+ */
241
+ ```
242
+
195
243
### ** ...**
196
244
197
245
```
Original file line number Diff line number Diff line change @@ -171,6 +171,54 @@ class MyQueue {
171
171
*/
172
172
```
173
173
174
+ ### ** TypeScript**
175
+
176
+ ``` ts
177
+ class MyQueue {
178
+ stack1: number [];
179
+ stack2: number [];
180
+ constructor () {
181
+ this .stack1 = [];
182
+ this .stack2 = [];
183
+ }
184
+
185
+ push(x : number ): void {
186
+ this .stack1 .push (x );
187
+ }
188
+
189
+ pop(): number {
190
+ if (! this .stack2 .length ) {
191
+ while (this .stack1 .length ) {
192
+ this .stack2 .push (this .stack1 .pop ());
193
+ }
194
+ }
195
+ return this .stack2 .pop ();
196
+ }
197
+
198
+ peek(): number {
199
+ if (! this .stack2 .length ) {
200
+ while (this .stack1 .length ) {
201
+ this .stack2 .push (this .stack1 .pop ());
202
+ }
203
+ }
204
+ return this .stack2 [this .stack2 .length - 1 ];
205
+ }
206
+
207
+ empty(): boolean {
208
+ return ! this .stack1 .length && ! this .stack2 .length ;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Your MyQueue object will be instantiated and called as such:
214
+ * var obj = new MyQueue()
215
+ * obj.push(x)
216
+ * var param_2 = obj.pop()
217
+ * var param_3 = obj.peek()
218
+ * var param_4 = obj.empty()
219
+ */
220
+ ```
221
+
174
222
### ** ...**
175
223
176
224
```
Original file line number Diff line number Diff line change
1
+ class MyQueue {
2
+ stack1 : number [ ] ;
3
+ stack2 : number [ ] ;
4
+ constructor ( ) {
5
+ this . stack1 = [ ] ;
6
+ this . stack2 = [ ] ;
7
+ }
8
+
9
+ push ( x : number ) : void {
10
+ this . stack1 . push ( x ) ;
11
+ }
12
+
13
+ pop ( ) : number {
14
+ if ( ! this . stack2 . length ) {
15
+ while ( this . stack1 . length ) {
16
+ this . stack2 . push ( this . stack1 . pop ( ) ) ;
17
+ }
18
+ }
19
+ return this . stack2 . pop ( ) ;
20
+ }
21
+
22
+ peek ( ) : number {
23
+ if ( ! this . stack2 . length ) {
24
+ while ( this . stack1 . length ) {
25
+ this . stack2 . push ( this . stack1 . pop ( ) ) ;
26
+ }
27
+ }
28
+ return this . stack2 [ this . stack2 . length - 1 ] ;
29
+ }
30
+
31
+ empty ( ) : boolean {
32
+ return ! this . stack1 . length && ! this . stack2 . length ;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Your MyQueue object will be instantiated and called as such:
38
+ * var obj = new MyQueue()
39
+ * obj.push(x)
40
+ * var param_2 = obj.pop()
41
+ * var param_3 = obj.peek()
42
+ * var param_4 = obj.empty()
43
+ */
You can’t perform that action at this time.
0 commit comments