题解 | #包含min函数的栈#单栈思路,min时全部出栈再入栈

包含min函数的栈

http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

import java.util.ArrayList;

public class Solution {

    private Stack<Integer> stack = new Stack();
    public void push(int node) {
        stack.push(node);
    }
    
    public void pop() {
        stack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
        ArrayList<Integer> list = new ArrayList<>();
        int min = stack.pop();
        list.add(min);
        while(!stack.isEmpty()){
            int pop  = stack.pop();
            min = Math.min(min,pop);
            list.add(0,pop);
        }
        for(int i = 0;i<list.size();i++){
            stack.push(list.get(i));
        }
        list.clear();
        return min;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
点赞 评论 收藏
分享
完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务