import java.util.*;
class getMin1{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public getMin1(){
this.stackData=new Stack<Integer> ();
this.stackMin=new Stack<Integer>();
}
public void push(int numData)
{ if(this.stackMin.isEmpty())
this.stackMin.push(numData);
if(numData<this.getMin())
this.stackMin.push(numData);
this.stackData.push(numData);
}
public int pop(){
if(this.stackMin.isEmpty())
throw new RuntimeException("the stack is empty");
int value=this.stackData.pop();
if(value==this.getMin())
this.stackMin.pop();
return value;
}
public int getMin()
{
if(this.stackMin.isEmpty())
throw new RuntimeException("your stack is empty");
return this.stackMin.peek();
}
}
构建一个栈,并返回其最小值
最新推荐文章于 2021-05-16 14:28:55 发布