题解 | #包含min函数的栈#

包含min函数的栈

https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49?tpId=295&tqId=23281&ru=%2Fexam%2Fcompany&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Fcompany

import java.util.ArrayList;
import java.util.Stack;

public class Solution {
  //数组模拟栈

    ArrayList<Integer> list = new ArrayList<>();
    int idx = -1;
    
    public void push(int node) {
        idx ++;
        list.add(node);
    }
    
    public void pop() {
        list.remove(idx--);
    }
    
    public int top() {
        return list.get(idx);
    }
    
    public int min() {
        return list.stream().min((o1,o2) -> {
            return o1-o2;
        }).get();
    }
}

全部评论

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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