-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
44 lines (38 loc) · 1.24 KB
/
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 TripleInOne {
private cap: number;
private stk: number[];
constructor(stackSize: number) {
this.cap = stackSize;
this.stk = Array<number>(stackSize * 3 + 3).fill(0);
}
push(stackNum: number, value: number): void {
if (this.stk[this.cap * 3 + stackNum] < this.cap) {
this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]] = value;
this.stk[this.cap * 3 + stackNum]++;
}
}
pop(stackNum: number): number {
if (this.isEmpty(stackNum)) {
return -1;
}
this.stk[this.cap * 3 + stackNum]--;
return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]];
}
peek(stackNum: number): number {
if (this.isEmpty(stackNum)) {
return -1;
}
return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum] - 1];
}
isEmpty(stackNum: number): boolean {
return this.stk[this.cap * 3 + stackNum] === 0;
}
}
/**
* Your TripleInOne object will be instantiated and called as such:
* var obj = new TripleInOne(stackSize)
* obj.push(stackNum,value)
* var param_2 = obj.pop(stackNum)
* var param_3 = obj.peek(stackNum)
* var param_4 = obj.isEmpty(stackNum)
*/