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

包含min函数的栈

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

import java.util.Stack;

public class Solution {

    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();

    
    public void push(int node) {
        if (stack1.isEmpty()) {
            stack1.push(node);
            stack2.push(node);
        } else {
            int val = stack2.peek();
            if (val < node) {
                stack2.push(val);
            } else {
                stack2.push(node);
            }
            stack1.push(node);
        }
    }
    
    public void pop() {
        if (stack1.isEmpty()) {
            throw new RuntimeException("The stack is empty");
        }
        stack1.pop();
        stack2.pop();
    }
    
    public int top() {
        if (stack1.isEmpty()) {
            throw new RuntimeException("The stack is empty");
        }
        return stack1.peek();
    }
    
    public int min() {
        if (stack2.isEmpty()) {
            throw new RuntimeException("The stack is empty");
        }
        return stack2.peek();
    }
}

全部评论

相关推荐

09-11 10:24
武汉大学 运营
点赞 评论 收藏
分享
沐芷凌曦:这简历数分别指望了,数分最基本的SQL能力你的经历是完全没办法佐证的,而且简历排版极其混乱。你的奖项为什么要写具体的项目内容;教育经历为什么要写你在什么课学到了什么东西,这些都应该是在专业技能里的;专业技能里你又把项目的内容放了进来,而且专业技能你又在强调ETL,如果说你确定要把ETL作为你专业技能的主体那你的经历为什么不能重点佐证呢;反而项目经历你项目等于你调用PyEcharts做了一个看板,就是最基本的课程设计,也是没办法佐证你对PyEcharts的掌握程度,而且没有说具体用什么技术做了什么东西中间做了什么最终得到了什么结果。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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