剑指offer(20)包含min函数的栈
import java.util.Stack;
import java.util.Iterator;
public class Solution {
Stack<Integer> stack = new Stack<Integer>();
public void push(int node) {
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
Iterator<Integer> iterator = stack.iterator();
int tmp = 0;
int min = stack.peek();//假设栈顶是最小的
while(iterator.hasNext()){
tmp = iterator.next();
if(min > tmp){
min = tmp;
}
}
return min;
}
}