定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.*;
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() {
int min = stack.peek();
for (Iterator<Integer> it = stack.iterator(); it.hasNext(); ) {
int temp = (Integer)it.next();
if (temp < min) min = temp;
}
return min;
}
}