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

包含min函数的栈

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

import java.util.Stack;

public class Solution {

    Stack <Integer> normal = new Stack<Integer>();
    Stack <Integer> min = new Stack<Integer>();
  
    public void push(int node) {
        normal.push(node);
        if(min.isEmpty()==true){
            min.push(node);
        }else {
            if (node<min.peek()){
                min.push(node);
            }else{
                min.push(min.peek());
            }
        }
    }
    
    public void pop() {
        int now = normal.pop();
        min.pop();
    }
    
    public int top() {
        return normal.peek();
    }
    
    public int min() {
        return min.peek();
    }
}
全部评论

相关推荐

01-12 17:45
门头沟学院 Java
985废物一枚:就是问问你能不能接受北京的房租,hr也知道公司工资不高,大概率是要贴钱的
找实习记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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