-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
41 lines (35 loc) · 1.07 KB
/
Solution.java
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
class TripleInOne {
private int cap;
private int[] stk;
public TripleInOne(int stackSize) {
cap = stackSize;
stk = new int[cap * 3 + 3];
}
public void push(int stackNum, int value) {
if (stk[cap * 3 + stackNum] < cap) {
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value;
++stk[cap * 3 + stackNum];
}
}
public int pop(int stackNum) {
if (isEmpty(stackNum)) {
return -1;
}
--stk[cap * 3 + stackNum];
return stk[cap * stackNum + stk[cap * 3 + stackNum]];
}
public int peek(int stackNum) {
return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1];
}
public boolean isEmpty(int stackNum) {
return stk[cap * 3 + stackNum] == 0;
}
}
/**
* Your TripleInOne object will be instantiated and called as such:
* TripleInOne obj = new TripleInOne(stackSize);
* obj.push(stackNum,value);
* int param_2 = obj.pop(stackNum);
* int param_3 = obj.peek(stackNum);
* boolean param_4 = obj.isEmpty(stackNum);
*/