LC 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 element in the stack
参考答案
class MinStack { private Node head;
/** initialize your data structure here. */
public MinStack() { } public void push(int x) {
if(head == null){
head = new Node(x,x);
}else{
head = new Node(x,Math.min(x,head.min),head);
}
} public void pop() {
head = head.next;
} public int top() {
return head.val;
} public int getMin() {
return head.min;
} private class Node{
int min;
int val;
Node next;
private Node(int val, int min){
this(val,min,null);
}
private Node(int val, int min, Node next){
this.val = val;
this.min = min;
this.next = next;
}
}
}
附加注释
Node
新建一个 Node,包含 val、min 和 next(下一个节点)。val 记录当前值,min是最小值,next是下一个节点。
push 函数
进入push函数,判断 Node head 的属性。
如果是初始化 Node,那么 val 和 min 都是 输入值 x。
如果不是初始化 Node,那么建立一个新的 head,val 为输入值x,min值 是 当前值和上一个head节点的min值,即min值将会从同一直继承下去, next 指向上一个head。Node 一直会在整个链条上更新。
pop函数
当前 head节点 往回退,最后的节点被剔除了。
LC 155 Min Stack的更多相关文章
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- Mybatis-Plus BaseMapper自动生成SQL及MapperProxy
目录 Spring+Mybatis + Mybatis-Plus 自定义无XML的sql生成及MapperProxy代理生成 问题产生背景 框架是如何使用 无Xml的SQL是如何生成生成及SQL长成什 ...
- jsp页面,使用Struts2标签,传递和获取Action类里的参数,注意事项。<s:a action><s:iterator><s:param>ognl表达式
在编写SSH2项目的时候,除了使用<s:form>表单标签向Action类跳转并传递参数之外,很更多时候还需要用到<s:a action="XXX.action" ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- 安装wget 、 wget命令
今天给服务器安装新LNMP环境时,wget 时提示 -bash:wget command not found,很明显没有安装wget软件包.一般linux最小化安装时,wget不会默认被安装. 可以通 ...
- 10分钟梳理MySQL核心知识点
数据库的使用,是开发人员的基本功,对它掌握越清晰越深入,你能做的事情就越多. 做业务,要懂基本的SQL语句:做性能优化,要懂索引,懂引擎:做分库分表,要懂主从,懂读写分离... 今天我们用10分钟,重 ...
- linux系统空间不足,不重启进程,清理僵尸文件。
问题:通过lsof |grep delete命令可以看到状态为delete的进程文件占用了较多的空间,导致系统磁盘空间不足,而du 命令看到的磁盘空间占用没那么高. 得到僵尸文件名称:catalina ...
- 华为鸿蒙OS能取代安卓吗?
先回答问题,不能,起码几年之内不存在这种可能.8月9日华为的开发者大会上,余承东说:鸿蒙是一款基于微内核的全场景分布式OS.鸿蒙OS的设计初衷是为满足全场景智慧体验的高标准的连接要求,为此华为提出了4 ...
- jQuery常用操作部分总结
注意:$(“.xxx”) 类,一定要在前面加上点callback为完成后执行的函数名称隐藏显示:hide() show()淡入淡出:fadeIn() fadeOut() fadetoggl ...
- php如何开启gd2扩展
extension=php_gd2.dll 找到php的配置文件php.ini,搜索extension=php_gd2.dll,去掉前面的分号即可:如果没有直接添加这种情况适合于windows系统和编 ...
- echarts.js导致angular-translate加载模块失败
echarts.js导致angular-translate加载模块失败,待解决