题目:https://leetcode-cn.com/problems/min-stack/
1.双栈解法
package www.bit.javase.OJ.Stack;
import java.util.Stack;
/**
*
* @author mayanni
* @date 2019-05-31 16:50
*/
class MinStack {
//双栈解法
//存放具体数值的栈
private Stack<Integer> dataStack;
//存放栈的最小元素
private Stack<Integer> minStack;
public MinStack() {
dataStack=new Stack<>();
minStack=new Stack<>();
}
public void push(int x) {
if(minStack.isEmpty()|| x <= minStack.peek()){
minStack.push(x);
}
dataStack.push(x);
}
public void pop() {
if(dataStack.peek().equals(minStack.peek())){
minStack.pop();
}
dataStack.pop();
}
public int top() {
return dataStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
public class MinStackTest{
public static void main(String[] args) {