-
Notifications
You must be signed in to change notification settings - Fork 8
/
MinStack.java
68 lines (59 loc) · 1.58 KB
/
MinStack.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package stack;
// Source : https://leetcode.com/problems/min-stack/
// Id : 155
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-06-13
// Topic : Design
// Level : Medium
// Other :
// Tips :
// Result : 78.13% 14.46%
import java.util.Stack;
public class MinStack {
private Stack<Integer> stack;
private 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())
minStack.push(x);
else
minStack.push(Math.min(getMin(), x));
}
public void pop() {
if (stack.isEmpty())
return;
stack.pop();
minStack.pop();
}
public int top() {
if (stack.isEmpty())
throw new RuntimeException("栈中元素为空,此操作非法");
return stack.peek();
}
/**
* 可以在每个元素 a 入栈时把当前栈的最小值 m 存储起来。
* 在这之后无论何时,如果栈顶元素是 a,我们就可以直接返回存储的最小值 m
*
* @return
*/
public int getMin() {
if (stack.isEmpty())
throw new RuntimeException("栈中元素为空,此操作非法");
return minStack.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/