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/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- AGC029C - Lexicographic constraints
记录我心路历程吧,这道小水题暴露出我很多问题. 给定 \(n\) 个字符串长度 \(a_i\) ,求字符集最小多大,才能构造出按字典序比较 \(s_1 < s_2 < \dots < ...
- tensorflow 框架图
- 如何使用PHP排序key为字母+数字的数组
你还在为如何使用PHP排序字母+数字的数组而烦恼吗? 今天有个小伙伴在群里问: 如何将一个key为字母+数字的数组按升序排序呢? 举个例子: $test = [ 'n1' => 22423, ' ...
- AVL平衡树(非指针实现)
看了网上三四篇博客,学习了AVL树维护平衡的方式.但感觉他们给出的代码都有一点瑕疵或者遗漏,懂得了思想之后,花了一些时间把他们几篇的长处结合起来,没有使用指针,实现了一下.每个小逻辑功能都抽象成了函数 ...
- checkbox为空
<view:qrytr> <view:qrytd width="15%" heightshow="true">请选择执法范围:</ ...
- java随机生成6位随机数 5位随机数 4位随机数
随机数,应用会相当广,验证数,订单号,流水号拼接. 下面是java随机数生成语句: 生成6位随机数(不会是5位或者7位,仅只有6位): System.+)*)); 同理,生成5位随机数: System ...
- git与github建立仓库连接步骤(纯小白教程)
一.先对git 进行用户设置 首先你得在网上下载git软件并且安装,一路默认安装就好了,然后就可以开始本地仓库的建立了.打开你安装好的git, 在开始菜单里面找到git文件夹里面的git bash端 ...
- Unity通过世界坐标系转换到界面坐标位置
public static Vector2 WorldToCanvasPoint(Canvas canvas, Vector3 worldPos) { Vector2 pos; RectTransfo ...
- python 利用python的subprocess模块执行外部命令,获取返回值
有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...
- koa 项目实战(九)passport验证token
1.安装模块 npm install koa-passport -D npm install passport-jwt -D 2.解析token 根目录/config/passport.js cons ...