package 栈相关.q155_最小栈;
import java.util.Stack;
/**
* 不使用辅助栈,每次push两个元素
*/
public class MinStack {
private Stack<Integer> stack;
public MinStack() {
stack = new Stack<>();
}
public void push(int x) {
if (stack.isEmpty()) {
stack.push(x);
stack.push(x);
} else {
int tmp = stack.peek();
stack.push(x);
if (tmp < x) {
stack.push(tmp);
} else {
stack.push(x);
}
}
}
public void pop() {
stack.pop();
stack.pop();
}
public int top() {
return stack.get(stack.size() - 2);
}
public int getMin() {
return stack.peek();
}
}
q155_最小栈
作品《LeetCode题目分类与面试问题整理 - q155_最小栈》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/5r3.html