Skip to content

Commit 5e2cc9e

Browse files
committed
feat: add typescript solution to lc problem: No.0232.Implement Queue using Stacks
1 parent 7b85851 commit 5e2cc9e

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

solution/0200-0299/0232.Implement Queue using Stacks/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,54 @@ class MyQueue {
192192
*/
193193
```
194194

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+
195243
### **...**
196244

197245
```

solution/0200-0299/0232.Implement Queue using Stacks/README_EN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,54 @@ class MyQueue {
171171
*/
172172
```
173173

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+
174222
### **...**
175223

176224
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
*/

0 commit comments

Comments
 (0)