包含min函数的栈

import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
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() {
int top = stack.pop();
stack.push(top);
return top;
}
public int min() {
List<Integer> list= new ArrayList<Integer>();
while(!stack.isEmpty()){
list.add(stack.pop());
}
for(int i=list.size()-1; i>=0; i--){
stack.push(list.get(i));
}
list.sort(new Comparator<Integer>(){
public int compare(Integer A, Integer B){
return A-B;
}
});
return list.get(0);
}
}

浙公网安备 33010602011771号