import java.util.Stack;java
//我的認爲這種方式有問題,假設把最小值出棧,再求最小值就有問題node
public class Solutioncode
{ Stack<Integer> s=new Stack<Integer>();it
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(); }
}io
//我的認爲下面這種方案更靠譜class
import java.util.Stack;import
import java.util.Iterator;im
public class Solutionnext
{ Stack<Integer> s=new Stack<Integer>();top
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; }
}