找出最小值
public class sAD01 {
private Stack<Integer> Data;
private Stack<Integer> Min;
public sAD01(){
this.Data = new Stack<Integer>();
this.Min = new Stack<Integer>();
}
public void push(int newNum){
if(this.Min.isEmpty()){
this.Min.push(newNum);
} else if(newNum <= this.getMin()){
this.Min.push(newNum);
}
this.Data.push(newNum);
}
public int pop(){
if(this.Data.isEmpty()){
throw new RuntimeException("your stack is empty");
}
int value = this.Data.pop();
if(value == this.getMin()){
this.Min.pop();
}
return value;
}
public int getMin(){
if(this.Min.isEmpty()){
throw new RuntimeException("your stack is empty");
}
return this.Min.peek();
}
}