-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain30.java
More file actions
51 lines (43 loc) · 1017 Bytes
/
Main30.java
File metadata and controls
51 lines (43 loc) · 1017 Bytes
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
package JZOffer2;
import java.util.LinkedList;
public class Main30 {
}
class MinStack {
LinkedList<Integer> stack;
LinkedList<Integer> aidStack;
/** initialize your data structure here. */
public MinStack() {
stack = new LinkedList<>();
aidStack = new LinkedList<>();
}
public void push(int x) {
if(!aidStack.isEmpty()){
if(x <= aidStack.peek()){
aidStack.push(x);
}else{
aidStack.push(aidStack.peek());
}
}else{
aidStack.push(x);
}
stack.push(x);
}
public void pop() {
if(!stack.isEmpty()){
stack.pop();
aidStack.pop();
}
}
public int top() {
if(!stack.isEmpty()){
return stack.peek();
}
return Integer.MAX_VALUE;
}
public int min() {
if(!aidStack.isEmpty()){
return aidStack.peek();
}
return Integer.MAX_VALUE;
}
}