题目
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
代码
import java.util.Stack;
import java.util.Iterator;
public class Solution {
Stack<Integer> head=new Stack<>();
public void push(int node) {
head.push(node);
}
public void pop() {
head.pop();
}
public int top() {
return head.peek();
}
public int min() {
int min=head.peek();
int temp=0;
Iterator<Integer> iterator=head.iterator();
while (iterator.hasNext()) {
temp=iterator.next();
if(min>temp)
min=temp;
}
return min;
}
}