155. Min Stack (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.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
minValues.push(INT_MAX);
}
void push(int x) {
if(x<=minValues.top()){
minValues.push(x);
}
allValues.push(x);
}
void pop() {
if(allValues.top()==minValues.top()){
minValues.pop();
}
allValues.pop();
}
int top() {
return allValues.top();
}
int getMin() {
return minValues.top();
}
private:
stack<int> allValues;
stack<int> minValues;
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
155. Min Stack (stack)的更多相关文章
- Java-JVM 栈帧(Stack Frame)
一.概述 栈帧位置 JVM 执行 Java 程序时需要装载各种数据到内存中,不同的数据存放在不同的内存区中(逻辑上),这些数据内存区称作运行时数据区(Run-Time Data Areas). 其中 ...
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- 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 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- leetcode 155. Min Stack --------- java
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 ...
- 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 ...
随机推荐
- idea 设置某项目路径下的文件在点击浏览器预览时的前缀
01,我们在开发 HTML 页面的时候,可以通过点击右上角的浏览器图标,直接打开浏览器访问,大大方便了开发 02,但是我们在开发 PHP 的时候,一般会自己安装集成环境或者编译环境,从上面的截图我们会 ...
- springMVC接受json并打开新页面
背景:框架中,两个web工程A,B,我的B工程开发了一个对外action接口,A来连,要实现的功能是,A的页面发起一个action请求,到达B的springmvc,通过验证后,打开一个B工程新的tab ...
- 在keil调用Notepad++
先打开keil, 新建一个 取名为notepad 选择notepad++的安装路径 设置参数 保持后可以看多了notepad的选项 运行当前的文件在notepad++打开
- CSS VISUAL RULES
CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...
- Grafana+Zabbix使用配置
官方提供的网友分享的图形面板,可以自行选择使用下载--- https://grafana.com/dashboards Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编 ...
- Linux awk命令使用方法
awk是linux上非常好用的文本处理工具,常用于指定列的处理,包括获取指定列的内容.根据指定列匹配关系输出等文本处理.本文主要描述awk命令的基本语法.正则表达式与操作符的使用.常用内置变量的含义和 ...
- webpack+avalon+mmState打包方案
终于到讲授如何整合avalon社区这个最强大的组件,基于状态机的路由系统了! 基于状态机的路由系统,据我所知,目前世界上只有三款,angular社区的ui-router, 网易出品的stateman, ...
- react设置innerHTML
<!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" ...
- hadoop-2.7.3完全分布式部署
一.环境介绍 IP host JDK linux版本 hadop版本 192.168.0.1 master 1.8.0_111 centos7.2.1511 hadoop-2.7 ...
- ubuntu14配置opencv3.4.1(转)
网站:https://blog.csdn.net/a1429331875/article/details/31539129 写此博客的目的是为了方便大家的学习,我是搞了半天,通过上网查找资料才成功的. ...