package java程序员面试笔试宝典;
import java.util.*;
public class 题8_2_3用O1时间复杂度求栈中最小元素 {
Stack<Integer> elem;
Stack<Integer> min;
public 题8_2_3用O1时间复杂度求栈中最小元素(){
elem=new Stack<Integer>();
min=new Stack<Integer>();
}
public int min(){
if(min.isEmpty()){
return Integer.MAX_VALUE;
}else {
return min.peek();
}
}
public int pop(){
int topData=elem.peek();
if(topData==this.min()){
min.pop();
}
elem.pop();
return topData;
}
public void push(Integer e){
elem.push(e);
if(min.isEmpty()){
min.push(e);
}else {
if(e<min.peek()){
min.push(e);
}
}
}
}