LeetCode——Min Stack
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.
带有minimum element属性的stack操作。
class MinStack {
public Node node = null;
public void push(int x) {
if(node == null) {
node = new Node(x);
node.min = x;
}
else {
Node tNode = new Node(x);
tNode.next = node;
node = tNode;
node.min = node.next.min < x ? node.next.min : x;
}
}
public void pop() {
node = node.next;
}
public int top() {
return node.val;
}
public int getMin() {
return node.min;
}
}
class Node {
int val;
int min;
Node next;
public Node(int val) {
this.val = val;
}
}
好久没1A了。
LeetCode——Min Stack的更多相关文章
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode() Min Stack 不知道哪里不对,留待。
class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...
- [leetcode] Min Stack @ Python
原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...
- [LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode Min Stack 最小值栈
题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
随机推荐
- 简单文件系统构建ramdisk
1. BusyBox编译工具,包含bin, sbin, usr, linuxrc. 2. 添加相关重要目录:dev, etc, mnt, proc, sys, lib, var, tmp. ...
- kill -HUP pid 更改配置后不重新启动服务,动态更新配置文件
kill -HUP pid kill -HUP pid pid 是进程标识.如果想要更改配置而不需停止并重新启动服务,请使用该命令.在对配置文件作必要的更改后,发出该命令以动态更新服务配置. 根据约 ...
- 浏览器向下兼容之polyfill[阅后即瞎]
我们入门JavaScript的时候都写过polyfill: 比如手写一个弹窗, 手动模拟实现一个表格, 这些魔力的对象都是浏览器原生支持的, 虽然当我成为JS专家之后再也没造过轮子, 但是最近才发现我 ...
- 总结golang之map
总结golang之map 2017年04月13日 23:35:53 趁年轻造起来 阅读数:18637 标签: golangmapgo 更多 个人分类: golang 版权声明:本文为博主原创文章, ...
- 『Golang』Martini框架入门
本文介绍golang中的优秀web开发框架martini! 序 Martini框架是使用Go语言作为开发语言的一个强力的快速构建模块化web应用与服务的开发框架.Martini是一个专门用来处理Web ...
- redis live 如何安装
在 windows 环境下安装 redislive 这是一篇在 windows 环境下安装 redislive 的教程! 项目地址:https://github.com/nkrode/RedisL ...
- WordCount示例深度学习MapReduce过程
转自: http://blog.csdn.net/yczws1/article/details/21794873 . 我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测 ...
- 关于Unity中UI中的Slider,Toggle和InputField等节点
一.Slider节点 1.创建一个Canvas 2.对Canvas进行一些初始化操作 3.创建一个Image的UI节点在Canvas下面作为子节点 4.把Image铺满整个Canvas,把宽高设置为6 ...
- 关于HTTP keep-alive的实验(转至 http://my.oschina.net/flashsword/blog/80037)
前面一篇文章提到,HTTP1.1中持久连接已经是默认配置,除非设置Connection为close,否则默认都会进行持久连接.但是我们知道事实标准跟教科书还是可能会有一定差距的,所以不妨自己尝试一下. ...
- u3d发布成全屏的方式
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public voi ...