包含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);

    }
}

 

posted @ 2022-11-27 11:06  northli  阅读(12)  评论(0编辑  收藏  举报