package QueueAndStack;
import java.util.Stack;
public class GetMinStack {
/**
* 设计一个有getMin功能的栈
* 就是一个栈中增加一个方法getMin()方法
*/
public static Stack<Integer> stack1 = new Stack<>();
public static Stack<Integer> stack2 = new Stack<>();
public static void push(Integer i){
stack1.push(i);
if(stack2.isEmpty() || i < stack2.peek()){
stack2.push(i);
}else{
stack2.push(stack2.peek());
}
}
public static Integer pop(){
if(stack1.isEmpty()){
throw new RuntimeException("栈中无元素");
}
stack2.pop();
return stack1.pop();
}
public static Integer getMin(){
if(stack1.isEmpty()){
throw new RuntimeException("栈中无元素");
}
return stack2.peek();
}
public static void main(String[] args){
GetMinStack stack = new GetMinStack();
stack.push(4);
stack.push(2);
stack.push(9);
System.out.print(stack.getMin());
}
}