forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
38 lines (33 loc) · 805 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
class SortedStack {
private stk: number[] = [];
constructor() {}
push(val: number): void {
const t: number[] = [];
while (this.stk.length > 0 && this.stk.at(-1)! < val) {
t.push(this.stk.pop()!);
}
this.stk.push(val);
while (t.length > 0) {
this.stk.push(t.pop()!);
}
}
pop(): void {
if (!this.isEmpty()) {
this.stk.pop();
}
}
peek(): number {
return this.isEmpty() ? -1 : this.stk.at(-1)!;
}
isEmpty(): boolean {
return this.stk.length === 0;
}
}
/**
* Your SortedStack object will be instantiated and called as such:
* var obj = new SortedStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.isEmpty()
*/