LeetCode155. 最小栈(java)

该博客介绍了LeetCode第155题的解决方案,如何设计一个同时支持push、pop、top操作,并能在常数时间内获取最小元素的栈。文章提供了两种不同的解法,并给出了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) – 将元素 x 推入栈中。
pop() – 删除栈顶的元素。
top() – 获取栈顶元素。
getMin() – 检索栈中的最小元素。

示例:
在这里插入图片描述
代码:

  • 解法一
class MinStack {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;
    /** 构造方法 */
    public MinStack() {
        stackData = new Stack<Integer>();
        stackMin = new Stack<Integer>();
    }
    
    /*将元素x推入栈中*/
    public void push(int x) {
        if(stackMin.empty()) {
            stackMin.push(x);
        } else if(x <= getMin()) {
            stackMin.push(x);
        } else {
            int newMin = getMin();
            stackMin.push(newMin);
        }
        stackData.push(x);
    }
    
    /*删除栈顶元素*/
    public void pop() {
        if(stackData.empty()) {
            throw new RuntimeException("your stack is empty!");
        }
        stackMin.pop();
        stackData.pop();
    }
    
    /*获取栈顶元素*/
    public int top() {
        if(stackData.empty()) {
            throw new RuntimeException("your stack is empty!");
        }
        return stackData.peek();
    }
    
    /*检索栈中的最小元素*/
    public int getMin() {
        if(stackMin.empty()) {
            throw new RuntimeException("your stack is empty!");
        }
        return stackMin.peek();
    }
}

  • 解法二
class MinStack {
    // 支持push,pop,top的栈
    private Stack<Integer> mainStack;
    // 支持在常数时间内检索到最小元素的单调栈
    private Stack<Integer> minStack;

    /** initialize your data structure here. */
    public MinStack() {
        this.mainStack = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(int x) {
        mainStack.push(x);
        // 实际上每次push只需要记录更小的值
        if(minStack.isEmpty() || minStack.peek() >= x)
            minStack.push(x);
    }
	//弹栈
    public void pop() {
        int tmp = mainStack.pop();
        if(tmp == minStack.peek())
            minStack.pop();
    }
	//获取栈顶元素
    public int top() {
        return mainStack.peek();
    }
	//获取最小元素
    public int getMin() {
        return minStack.peek();
    }
}

  • 别人的代码
class MinStack {
    private Deque<Integer> mainStack; // 主栈,存储所有数据
    
    private Deque<Integer> minStack; // 将数据压入主栈时,存储最小值

    /** initialize your data structure here. */
    public MinStack() {
        mainStack = new ArrayDeque<>();
        minStack = new ArrayDeque<>();
    }
    
    public void push(int x) {
        mainStack.push(x);
        
        Integer min = minStack.peek();
        if (min == null || x <= min) {
            minStack.push(x);
        }
    }
    
    public int pop() {
        int cur = mainStack.pop();
        if (cur == minStack.peek()) {
            minStack.pop();
        }
        return cur;
        
    }
    
    public int top() {
        return mainStack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值