[leetcode] 155. 最小栈

LeetCode.155 最小栈

public class MinStack {

	Stack<Integer> elements;
	Stack<Integer> min;

	MinStack() {
		elements = new Stack<Integer>();
		min = new Stack<Integer>();
	}

	public int getMin() {
		return min.peek();
	}

	public void push(int x) {
		elements.push(x);
		if (min.isEmpty() || getMin() > x) {
			min.push(x);
		} else {
			min.push(getMin());
		}
	}

	public int pop() {
		min.pop();
		return elements.pop();
	}

	int top() {
		return elements.peek();
	}
}
posted @ 2019-01-18 00:16  ACBingo  阅读(88)  评论(0编辑  收藏