package first;
import java.util.Stack;
public class MyStack1{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
MyStack1(){
stackData=new Stack<Integer>();
stackMin=new Stack<Integer>();
}
public void push(int newNum){
this.stackData.push(newNum);
if(this.stackMin.isEmpty()){
this.stackMin.push(newNum);
}else if(newNum<=this.getMin()){
this.stackMin.push(newNum);
}
}
public int pop(){
int value=this.stackData.pop().intValue();
if(value==this.stackMin.peek()){
this.stackMin.pop();
}
return value;
}
public int getMin(){
if(this.stackMin.isEmpty()){
throw new RuntimeException("your stack is empty!");
}
return this.stackMin.peek();
}
}
import java.util.Stack;
public class MyStack1{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
MyStack1(){
stackData=new Stack<Integer>();
stackMin=new Stack<Integer>();
}
public void push(int newNum){
this.stackData.push(newNum);
if(this.stackMin.isEmpty()){
this.stackMin.push(newNum);
}else if(newNum<=this.getMin()){
this.stackMin.push(newNum);
}
}
public int pop(){
int value=this.stackData.pop().intValue();
if(value==this.stackMin.peek()){
this.stackMin.pop();
}
return value;
}
public int getMin(){
if(this.stackMin.isEmpty()){
throw new RuntimeException("your stack is empty!");
}
return this.stackMin.peek();
}
}