q155_最小栈

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();
    }
}