AcWing 41. 包含min函数的栈
原题链接
简单
作者:
joyonline
,
2019-12-17 13:11:45
,
所有人可见
,
阅读 848
java 代码
class MinStack {
public Stack<Integer> a;
public Stack<Integer> c;
/** initialize your data structure here. */
public MinStack() {
a = new Stack();
c = new Stack();
}
public void push(int x) {
a.push(x);
if(c.isEmpty()){
c.push(x);
return;
}
c.push(Math.min(x,c.peek()));
}
//删除第一个元素
public void pop() {
int x = a.pop();
while(!c.isEmpty() && x == c.peek()){
c.pop();
}
}
//返回第一个元素
public int top() {
return a.peek();
}
public int getMin() {
return c.peek();
}
}