import java.util.Stack;
//个人认为这种方式有问题,假设把最小值出栈,再求最小值就有问题
public class Solution
{ Stack<Integer> s=new Stack<Integer>();
Stack<Integer> ms=new Stack<Integer>();
public void push(int node)
{
if(ms.isEmpty()||node<ms.peek())
ms.push(node);
s.push(node);
}
public void pop()
{
if(ms.peek()==s.peek())
ms.pop();
s.pop();
}
public int top()
{
return s.peek();
}
public int min()
{
return ms.peek();
}
}
//个人认为下面这种方案更靠谱
import java.util.Stack;
import java.util.Iterator;
public class Solution
{ Stack<Integer> s=new Stack<Integer>();
public void push(int node)
{
s.push(node);
}
public void pop()
{ s.pop();
}
public int top()
{
return s.peek();
}
public int min()
{
int max = Integer.MAX_VALUE;
Iterator it=s.iterator();
while(it.hasNext()){
int temp=(int)it.next();
if(temp<max)
max=temp;
}
return max;
}
}