forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
44 lines (37 loc) · 827 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class MyQueue {
stk1: number[];
stk2: number[];
constructor() {
this.stk1 = [];
this.stk2 = [];
}
push(x: number): void {
this.stk1.push(x);
}
pop(): number {
this.move();
return this.stk2.pop();
}
peek(): number {
this.move();
return this.stk2[this.stk2.length - 1];
}
empty(): boolean {
return !this.stk1.length && !this.stk2.length;
}
move(): void {
if (!this.stk2.length) {
while (this.stk1.length) {
this.stk2.push(this.stk1.pop());
}
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/