包含min函数的栈

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

public class Solution {
// 1 2 3 4 -1
Stack<integer> stackAll = new Stack<>();
// 1 1 1 1 -1
Stack<integer> min = new Stack<>();</integer></integer>

public void push(int node) {
    if(!stackAll.isEmpty() && node > min.peek()){
       node = min.peek();
    }

    stackAll.push(node);
    min.push(node);
}

public void pop() {
    stackAll.pop();
    min.pop();
}

public int top() {
    return stackAll.peek();
}

public int min() {
    return min.peek();
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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