前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Min Stack

Min Stack

作者头像
Tyan
发布于 2019-05-25 15:09:15
发布于 2019-05-25 15:09:15
66800
代码可运行
举报
文章被收录于专栏:SnailTyanSnailTyan
运行总次数:0
代码可运行

1. 问题描述

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum element in the stack.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

2. 求解

主要是模拟写一个最小栈。要注意push时可能会输入null。需要使用双栈实现,一个保存数据,一个保存最小值。由于随着数据出栈,最小值是不断变化的,因此需要一个最小值栈来保存最小值。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class MinStack {
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    public void push(int x) {
        if(minStack.isEmpty() || x <= minStack.peek()) {
            minStack.push(x);
        }
        stack.push(x);
    }

    public void pop() {
        if(stack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        stack.pop();
    }

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

    public int getMin() {
        return minStack.peek();        
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年03月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【LeetCode每日打卡】155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
韩旭051
2020/06/22
3000
LeetCode笔记:155. Min Stack
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2.
Cloudox
2021/11/23
1780
155.Min Stack(Stack-Easy)
该文介绍了如何设计一个支持push、pop、top和返回最小值的stack,同时使用两个stack来保存所有元素和最小元素,实现了在常数时间内检索到最小元素的功能。
Jack_Cui
2018/01/08
5610
​LeetCode刷题实战155:最小栈
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/01/19
2540
LeetCode 0155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Reck Zhang
2021/08/11
2240
LeetCode 155:最小栈 Min Stack
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
爱写bug
2019/08/02
4970
Leetcode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum elemen
triplebee
2018/01/12
6730
Q155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element
echobingo
2018/04/25
4620
leetcode-155-Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e
chenjx85
2018/05/21
5990
155. 最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
张伦聪zhangluncong
2022/10/26
1780
【小Y学算法】⚡️每日LeetCode打卡⚡️——41. 最小栈
设计一个支持 push ,pop,top操作,并能在常数时间内检索到最小元素的栈。
呆呆敲代码的小Y
2021/09/29
2690
【小Y学算法】⚡️每日LeetCode打卡⚡️——41. 最小栈
【Java数据结构】详解Stack与Queue(二)
除此之外我们还可以用另一种特殊方法,就是利用栈去打印,代码展示在这。相比递归其更高效。
E绵绵
2024/06/04
1200
【Java数据结构】详解Stack与Queue(二)
155. 最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 * * push(x) —— 将元素 x 推入栈中。 * pop() —— 删除栈顶的元素。 * top() —— 获取栈顶元素。 * getMin() —— 检索栈中的最小元素。
名字是乱打的
2021/12/23
1860
☆打卡算法☆LeetCode 155. 最小栈 算法解析
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
恬静的小魔龙
2022/08/07
3090
☆打卡算法☆LeetCode 155. 最小栈 算法解析
LeetCode-155. 最小栈(java)
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 
bug菌
2023/05/27
1930
LeetCode-155. 最小栈(java)
leetcode栈之最小栈
这里再push的时候,计算了min值,同时再min值有更新的时候,先push了上一个min值,最后再push当前的min值;在pop的时候,判断是否等于min值,等于的话,再pop一次,更新当前min值为上一个min值,这里这样子实现是基于题目的假设(pop、top 和 getMin 操作总是在 非空栈 上调用)以及MinStack的调用顺序。
code4it
2020/10/04
3060
leetcode栈之最小栈
数据结构与算法(2)——栈和队列栈队列LeetCode 相关题目整理其他题目整理
栈是一种用于存储数据的简单数据结构(与链表类似)。数据入栈的次序是栈的关键。可以把一桶桶装的薯片看作是一个栈的例子,当薯片做好之后,它们会依次被添加到桶里,每一片都会是当前的最上面一片,而每次我们取的时候也是取的最上面的那一片,规定你不能破坏桶也不能把底部捅穿,所以第一个放入桶的薯片只能最后一个从桶里取出;
我没有三颗心脏
2018/07/24
1.3K0
每日一刷:最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
乐心湖
2021/03/02
2800
每日一刷:最小栈
【LeetCode】最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
Delevin
2019/01/24
5830
【LeetCode】最小栈
[算法题] 实现一个特殊的栈,在基本栈的基础上,增加返回栈中最小元素的方法
实现一个特殊的栈,在栈的基本功能的基础上,增加一个功能:返回栈中最小元素 要求:
CoderJed
2019/05/14
7930
[算法题] 实现一个特殊的栈,在基本栈的基础上,增加返回栈中最小元素的方法
相关推荐
【LeetCode每日打卡】155. Min Stack
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验