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

包含min函数的栈

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

import java.util.Stack;

public class Solution {
    public Stack<Integer> s1 = new Stack<>();
    public Stack<Integer> s2 = new Stack<>();
    
    public void push(int node) {
        int min = node;
        if(!s2.isEmpty()) {
            min = Math.min(s2.peek(), min);
        }
        s1.push(node);
        s2.push(min);
    }
    
    public void pop() {
        s1.pop();
        s2.pop();
    }
    
    public int top() {
        return s1.peek();
    }
    
    public int min() {
        return s2.peek();
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:01
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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