-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathThreeInOneLCCI.java
More file actions
59 lines (52 loc) · 1.46 KB
/
ThreeInOneLCCI.java
File metadata and controls
59 lines (52 loc) · 1.46 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package stack;
// Source : https://leetcode.com/problems/three-in-one-lcci/
// Id : mst03.01
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2022/2/9
// Topic : stack
// Level : Easy
// Other :
// Tips :
// Links :
// Result : 91.52% 5.64%
import java.util.LinkedList;
public class ThreeInOneLCCI {
// 9 ms
int[] stack;
int[] pointers;
int stackSize;
public ThreeInOneLCCI(int stackSize) {
// public TripleInOne(int stackSize) {
this.stack = new int[stackSize * 3];
this.pointers = new int[3];
pointers[1] = stackSize;
pointers[2] = stackSize * 2;
this.stackSize = stackSize;
}
public void push(int stackNum, int value) {
if (pointers[stackNum] != (1 + stackNum) * stackSize) {
stack[pointers[stackNum]] = value;
pointers[stackNum]++;
}
}
public int pop(int stackNum) {
if (pointers[stackNum] == stackNum * stackSize) {
return -1;
} else {
int val = stack[pointers[stackNum] - 1];
pointers[stackNum]--;
return val;
}
}
public int peek(int stackNum) {
if (pointers[stackNum] == stackNum * stackSize) {
return -1;
} else {
int val = stack[pointers[stackNum] - 1];
return val;
}
}
public boolean isEmpty(int stackNum) {
return pointers[stackNum] == stackNum * stackSize;
}
}