-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy path28.java
More file actions
51 lines (27 loc) · 640 Bytes
/
28.java
File metadata and controls
51 lines (27 loc) · 640 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
class MinStack {
Stack<Integer> stack;
Stack<Integer> minstack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack();
minstack = new Stack();
}
public void push(int x) {
stack.push(x);
if(minstack.isEmpty()||x<=minstack.peek()){
minstack.push(x);
}else{
minstack.push(minstack.peek());
}
}
public void pop() {
stack.pop();
minstack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return minstack.peek();
}
}