【LeetCode Easy】155 Min Stack

Livox-avia+FAST-LIO麻瓜使用教程 激光雷达 SLAM Fastlio 安装 阅读详情

原题链接

Description:

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.

My Solution:

  1. 最先想到用两个栈(栈A栈B),当A栈非空时,以A栈栈顶为基准,大于或等于A栈栈顶的数字push进B栈,小于A栈栈顶的数字push进A栈。
    这个方法遇到了两个问题:
    (1)分开来push的话进栈的相对顺序就乱了,会给pop()方法带来问题,所以不管数字push进哪一个栈,同时也要给另一个栈push,另一个栈的push的内容要是一个不可能出现的值,这样可以保留相对位置
    (2)由第一个问题的解决方案带来的第二个问题,要getMin的时候肯定是去栈A找,但栈A的栈顶很有可能是我为了保持相对位置而push进去的那个“不可能出现的值”

  2. 第二个问题难以解决,于是重新想了办法,仍旧是用两个栈,栈A就用作普通栈,栈B专门用来存相对较小值,当栈A非空时,若push的值小于栈A的栈顶值,就同时将这个值push进两个栈,那么栈B里面从栈顶到栈底就是压栈过程中不同时刻的最小值都保存了(并且有顺序)
    代码如下:

class MinStack {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    
    /** initialize your data structure here. */
    public MinStack() {
        this.stack1 = new Stack<>();
        this.stack2 = new Stack<>();
    }
    
    public void push(int x) {
        if (stack2.isEmpty() || stack2.peek() >= x){
            stack2.push(x);
        }
        stack1.push(x);
    }
    
    public void pop() {
        /*if(stack1.peek() == stack2.peek()){
            stack2.pop();
        }
        stack1.pop();*/
        //注意这里!为什么不能用上面注释掉的代码(见summary and knowledge)
        int tmp = stack2.peek();
        if (stack1.peek() == tmp) {
            stack2.pop();
        }
        stack1.pop();
    }
    
    public int top() {
        return stack1.peek();
    }
    
    public int getMin() {
        return stack2.peek();
    }
}

Other Solutions:

  1. 只用一个栈,另用一个int型成员变量来存最小值:
    (最初看到这个方法的时候想到一个问题,如果我把最小值pop出去了,只有一个int型变量的话我怎么找到原最小值删除之后的最小值呢?下面的code中的解决方法是每次在找到新的最小值的时候都把原来的最小值重新入栈一次,那么若pop的值正好为当前最小值时,pop之后的栈顶就应该是删除当前最小值后所有值中的最小值)
class MinStack {
    int min = Integer.MAX_VALUE;
    Stack<Integer> stack = new Stack<Integer>();
    public void push(int x) {
        // only push the old minimum value when the current 
        // minimum value changes after pushing the new value x
        if(x <= min){          
            stack.push(min);
            min=x;
        }
        stack.push(x);
    }

    public void pop() {
        // if pop operation could result in the changing of the current minimum value, 
        // pop twice and change the current minimum value to the last minimum value.
        if(stack.pop() == min) min=stack.pop();
    }

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

    public int getMin() {
        return min;
    }
}

Summary and Knowledge

  1. 做这题的时候存在审题不清的情况,getMin()方法究竟是只要返回最小值,还是返回最小值并删除栈中的最小值?(但是想想easy题也不会要删除,尤其还要求要常数时间的复杂度
  2. 为什么pop()方法不能用被注释掉的那些代码:
    在reference第一个网址中提到的,peek()返回的是object类型,但其实这是jdk1.4之前的了,1.5之后peek()方法返回的就是其stack中存的item的类型(如该题中的Integer),问题主要是出在返回的类型是普通对象类型Integer,而不是primitive type中的int类型,此时我们拿到的是引用而不是具体的值,用“==”只会比较他们是否指向同一块内存,自然不可。

Reference

https://www.cnblogs.com/grandyang/p/4091064.html
https://stackoverflow.com/questions/3637936/java-integer-equals-vs
https://leetcode.com/problems/min-stack/discuss/49014/Java-accepted-solution-using-one-stack

第 0 篇|开篇:为什么我们要从 RNN 讲到 Transformer? 本文介绍了NLP模型的发展历程,从早期规则式方法到如今Transformer模型的演进脉络。文章强调理解NLP技术需要从基础模型入手,重点分析了传统机器学习的局限性(无法处理变长有序输入和深层语义关联),以及RNN如何通过循环结构首次实现顺序依赖捕捉。作者指出Transformer之所以成为主流,是因为它通过自注意力机制解决了RNN的核心痛点(低效串行处理和长距离依赖问题)。专栏将采用"从问题到解决方案"的讲解方式,帮助读者建立完整的NLP知识体系,避免孤立学习和盲目追新。全文旨在为初学 阅读详情

相关推荐

Copula函数

Copula是处理统计中随机变量相关性问题的一种方法。本文是对Copula函数的各种定义,Copula函数的思想,Copula函数的性质,Copula函数的类型等理论资料总结

Explore 5万+

[Leetcode 155, Easy] Min Stack

Leetcode, Easy

算法的实践 487

三甲医院网络架构与安全建设实战

华为OceanStor] [华为OceanStor][PACS存储双活] [HIS数据库同步]患者隐私 数据脱敏 天清Web防火墙动态脱敏策略。主数据中心 --[OTV专线]-- 备数据中心。实现医疗业务网/卫生专网/互联网三网隔离。核心业务配置(华为CE6865)自动备份配置(每天02:00)安全事件响应(30分钟SLA)漏洞扫描(每周日00:00)实现医疗物联网统一接入管控。五、等保2.0合规关键配置。堡垒机登录(双因素认证)四、医疗物联网安全方案。

有小小的远大抱负 1911

leetcode 155. Min Stack (easy)

利用两个容器,一个容器STA存储正常stack的节点数值,另一个容器MIN存储以STA每一层为栈顶时的最小值 class MinStack { private: vector<int> min; vector<int> sta; public: /** initialize your data structure here. */ MinStack() ...

Rh's blog 164

手把手教你用OPA运放和MOS管搭建恒流源电路(附完整电路图)

本文详细介绍了如何使用OPA运放和MOS管搭建高精度恒流源电路,涵盖核心元器件选型、电路设计、工作原理及实际搭建步骤。通过负反馈控制机制和MOS管工作区设置技巧,实现稳定的电流输出,适用于LED驱动、电池充电等场景。附完整电路图和调试技巧,助您快速掌握恒流源设计。

weixin_29239279的博客 535

逆向解析M团外卖mtgsig与waimai_sign加密参数实战指南⊙ω⊙

本文详细解析了M团外卖App中mtgsig与waimai_sign两个核心加密参数的逆向分析与生成方法。通过抓包定位、静态逆向与Frida动态调试,拆解了waimai_sign的RSA签名逻辑与mtgsig的复杂参数组装加密流程,并提供了Python模拟实现的框架与调试心得,为安全研究及合规数据分析提供实战指南。

weixin_29327525的博客 1284

LeetCode - Easy - 155. Min Stack

Topic Stack Design Description https://leetcode.com/problems/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 stac

KISS 238

YOLO26训练教程,训练自己的数据集教学,从源码下载,配置虚拟环境,准备数据集、训练、验证、推理测试 。实现0到1的完整教学过程。快速入门必看

本文详细介绍了YOLO26目标检测模型的配置与使用流程:1)下载YOLOv11源码并创建Python3.10虚拟环境;2)安装PyTorch2.5.1及相关依赖库;3)准备数据集并修改配置文件;4)编写训练脚本设置参数(640x640分辨率、500轮次);5)验证模型效果;6)使用训练好的模型进行推理测试;7)生成可视化热力图。整个过程涵盖了从环境搭建到模型应用的全流程,并提供了清华镜像源加速下载。

Ai缝合怪、助力小伙伴高效发论文 5285

【LeetCode】155. Min Stack 最小栈(Easy)(JAVA)

【LeetCode】155. Min Stack 最小栈(Easy)(JAVA) 题目地址: https://leetcode.com/problems/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 elemen

qq_16927853的博客 228

AI Agent智能体在Android开发中的应用

本文探讨了AIAgent(智能代理体)在Android开发中的应用,涵盖技术原理、实践案例与未来趋势。AIAgent通过机器学习、自然语言处理等技术实现代码生成、测试自动化及性能优化,显著提升开发效率。例如,强化学习模型可优化APP响应时间(公式:$\text{响应时间}=f(\text{负载},\text{资源})$),自动化测试覆盖率提升至95%。

郑伟强dev的专栏 124

Go基础:Go语言详细介绍,环境搭建,及第一个程序详解

Go语言是Google开发的开源编程语言,具有高效、简洁、并发性强等特点。文章从Go语言的诞生背景、核心特性及应用领域展开介绍,并详细说明了Windows、Linux和macOS下的环境搭建步骤,包括安装、环境变量配置及测试方法。最后通过“Hello, World!”程序解析Go语言基本语法,并演示了编译发布和跨平台编译功能。全文涵盖了Go语言从入门到实践的完整流程,适合初学者快速上手。

数据知道的博客 6800

Leetcode 155. Min Stack (Easy) (cpp)

Leetcode 155. Min Stack (Easy) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 707

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【leetcode】155. Min Stack(easy)

在栈的结构上多加一个getMin方法,返回栈中的最小值。 使用两个栈,一个来保存原有的值,另外一个初始push进去第一个值,当push第二个的时候,检查是不是比第一个小,如果比第二个栈的栈顶小,那么就push进去。 // 155 Min Stack class MinStack{ /** initialize your data structure here. */ public: MinSt...

eclipse_c 的专栏 203

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【Leetcode-Easy-155】 Min Stack

【Leetcode-Easy-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

LYZ 806

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Leetcode - 155 - Min Stack (easy)

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 ele

xin_rong的博客 240

【leetcode】155(Easy). Min Stack

解题思路: 没什么解题思路,烦死了这种题 提交代码: class MinStack { /** initialize your data structure here. */ Stack<Integer> st = new Stack<Integer>(); int min = Integer.MAX_VALUE; public MinStack() { }...

AXIMI的博客 256

[LeetCode] 155. Min Stack_Easy tag: 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() -- ...

weixin_30418341的博客 74

leetcode_155_Min Stack(C++)(easy)

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

u010431315的博客 415

Leetcode-Easy 155. Min Stack

234. Palindrome Linked List 描述: 栈的实现 思路: 通过列表进行实现 代码 class MinStack: def __init__(self): """ initialize your data structure here. """ ...

yanqianglifei的专栏 253

LeetCode Top 100 Liked Questions 155. Min Stack (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 155. Min Stack (Java版; Easy) 题目描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push...

littlehaes的博客 157

【LeetCode】(C#)155. Min Stack(Easy)

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

YvelineLong的博客 267

155. Min Stack [easy] (Python)

题目链接https://leetcode.com/problems/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()

Coder_Orz的博客 3863

155. Min Stack(easy)

Easy 1647172FavoriteShare 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 to...

SourDumplings的博客 211

leetcode 155. Min Stack

leetcode 155. Min Stack (Easy) 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 to...

Night_Thief的博客 231

leetcode 155.min-stack 最小栈 python3

时间:2020-5-19 题目地址:https://leetcode-cn.com/problems/min-stack/description/ 题目难度:Easy 题目描述: 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) —— 将元素 x 推入栈中。 pop()—— 删除栈顶的元素。 top()—— 获取栈顶元素。 getMin() —— 检索栈中的最小元素。 示例: 输入: ["MinStack","push","push",..

isabloomingtree的博客 236

Leetcode 155. Min Stack

easy 难度的题目, 运用一个辅助栈来存储当前最小的元素, 思路简单 : class MinStack { private Stack<Integer> dataStack; private Stack<Integer> minStack; int minValue; /** initialize your data structure...

mrbm_lj的博客 129
上一篇: 【Algorithm·fourth edition】二项分布的递归与非递归实现
下一篇: 【洛谷 · for C++】P1422 小玉家的电费
JellyFishDing
博客等级 码龄8年 4粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值