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/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- 在Postman脚本中发送请求(pm.sendRequest)
Postman的Collection(集合)/Folder(集合的子文件夹)/Request(请求)都有Pre-request script和Tests两个脚本区域, 分别可以在发送请求前和请求后使用 ...
- linux系统nginx下反向代理解析二级目录泛目录教程
解析规则1: location /目录名 { proxy_pass http://ip/目录名; } 解析规则2: location /目录名{ ...
- Python字典元素的增加删除和取出字典所有的键和值
一.增加一个或多个元素 d = {'a': 1} d.update(b=2) #也可以 d.update({‘b’: 2}) print(d) # {'a': 1, 'b': 2} d['e'] = ...
- js off动画事件
每个假期都过得如此快10月一是2017年最后一个假期.不由感叹时间过得真快.我已上个月离职,一直在家休整,今天得空吧前几天学习的知识真理一下. 今天主要整理关于,offset系列的,动画是咱们全都工作 ...
- CF258B
CF258B 题意: 7个人在 $ [1,m] $ 区间内取数,求第一个人选的数的4和7的个数大于其他人的4和7个数总和的方案数. 解法: 要求输入的 $ m $ 可以很大,而且需要按位考虑每隔人的贡 ...
- java将PDF的前几页拆出来组成新pdf
/** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * @param pdfFile 需要分割的PDF * @param savepath 新PDF * @param fro ...
- dubbo中的group与version的存在意义
公司每周五都要给线上系统发布一个版本,我将本周新开发的业务模块直接提交到svn的主干上(当然本机已经测试通过),在公司的测试环境部署运行正常,测试人员业务测试通过.但是在部署到准生产环境上后出现了意想 ...
- python 列表切片之负数的含义代码示例
a = list(range(10)) print(a[::]) #复制一个列表 print(a[::2]) #每隔2个取一次 print(a[::3]) #每隔3个取一次 print(a[::-1] ...
- 胜利点 final发布
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/10062 1.视频地址:https://www.bilibili.com/ ...
- JDK与CGlib动态代理的实现
应用的原型为 执行者:房屋中介Agency(分为JDKAgency.CGlibAgency) 被代理对象:程序员Programmer 被代理对象的实现接口:租户Tenement(CGlibAgency ...