-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0155.java
More file actions
63 lines (55 loc) · 1.57 KB
/
Copy pathLeetCode0155.java
File metadata and controls
63 lines (55 loc) · 1.57 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
60
61
62
63
/* Min Stack
* Example:
* Input: ["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
* Output: [null,null,null,null,-3,null,0,-2]
* */
import java.util.Stack;
public class LeetCode0155 {
public static void main(String args[]){
MinStack minStack = new MinStack();
minStack.push(512);
minStack.push(-1024);
minStack.push(-1024);
minStack.push(512);
minStack.pop();
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.top());
minStack.pop();
System.out.println(minStack.getMin());
}
}
class MinStack {
/** initialize your data structure here. */
private Stack<Integer> stack;
private int min;
public MinStack() {
stack = new Stack();
min = Integer.MAX_VALUE;
}
public void push(int x) {
//存储顺序为(底)x min x min x min ...(顶)
min = stack.size() > 0 ? Math.min(x, stack.peek()) : x;
stack.push(x);
stack.push(min);
}
public void pop() {
if(stack.pop() == stack.peek()){
//如果最小值被出栈,则更新当前最小值
stack.pop();
min = stack.peek();
}
else
stack.pop();
}
public int top() {
int temp = stack.pop();
int top = stack.peek();
stack.push(temp);
return top;
}
public int getMin() {
return stack.peek();
}
}