class MinStack {
private Stack<Integer> S;
private Stack<Integer> minS;
/** initialize your data structure here. */
public MinStack() {
S = new Stack<Integer>();
minS = new Stack<Integer>();
}
public void push(int x) {
S.push(x);
if(minS.isEmpty() || minS.peek() >= x) minS.push(x);
}
public void pop() {
int x = S.pop();
if(minS.peek() == x) minS.pop();
}
public int top() {
return S.peek();
}
public int getMin() {
return minS.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();
*/
力扣leetcode 155. 最小栈 java
最新推荐文章于 2024-07-14 02:25:25 发布