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/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- Meathill的博客地址
https://blog.meathill.com/ 安装mysql: https://blog.meathill.com/tech/setup-windows-subsystem-linux-for ...
- Java异常Error和Exception
简述 程序运行时,发生了不被期望的结果,阻止了程序按照预期正常执行,这就是异常.世界上没有不出错的程序,只有正确处理好意外情况,才能保证程序的可靠性. Java 语言在设计之初就提供了相对完善的异常处 ...
- mitmproxy修改二级代理
第一步 mitmweb --mode upstream:http://114.240.101.242:5672 -s server.py 第二步 def request(self, flow: mit ...
- 预处理、const、static与sizeof-为什么不把所有的函数都定义成内联函数
1:内联是以代码膨胀(复制)为代价的,仅仅省去了函数调用的开销,从而提高函数的执行效率.如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收获会很小.另一方面,每一处内联函数的调用都要复制 ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- windows下手动安装composer
原文地址:http://www.cnblogs.com/JANCHAN/p/7735882.html 1.下载compser.phar 地址 https://getcomposer.org/downl ...
- Vue入门下
使用npm创建项目,系统会自动生成一些列文件. 以慕课网上的Travel项目来说,在生成的项目文件中存在src文件夹,这个文件夹也是平时在做项目的时候用的比较多的,其他的一些配置信息更改的频率较低. ...
- HearthBuddy版本收集
Hearthbuddy-20190811-010-0b563c92.exe 20190810-003 SHA-256: b2a03c10124b038d2c48279cc50947907a55c8 ...
- MySQL有四种BLOB类型
先说明一下Blob的类型,直接从网上摘抄了!!!1.MySQL有四种BLOB类型: ·tinyblob:仅255个字符 ·blob:最大限制到65K字节 ·mediumblob:限制到16M字节 ·l ...
- js内存空间及this关键词详解
http://mp.weixin.qq.com/s/FYFepXmkzzDYNLKhpovYFA