Solution
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int x) {
stack.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) {
minStack.push(x);
}
}
public void pop() {
if (!stack.isEmpty()) {
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.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();
*/
Problem#2
* 对于类里面变量的用法不熟悉
* 有只用一个stack的方法,不存最小值,而把最小值与当前值的差值栈
Problem#1
* 完全没有概念,把栈,队列,背包的实现再熟悉一下