【一天一道LeetCode】#155. Min Stack
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:实现一个最小栈,在常量时间内完成push,pop,top和getmin等操作
解题思路:用两个栈,一个栈存储所有的数字,另一个栈存储最小数。
具体思路见代码:
class MinStack {
public:
/** initialize your data structure here. */
stack<int> datastack;//存储数据
stack<int> minstack;//存储最小栈
MinStack() {
}
void push(int x) {
datastack.push(x);//压入数据
if(minstack.empty()) minstack.push(x);//如果最小栈为空直接压入
else if(x<=minstack.top()) minstack.push(x);//如果当前压入的值小于等于最小栈的栈顶元素,则压入最小栈
}
void pop() {
if(datastack.top()==minstack.top()) minstack.pop();//如果数据栈和最小栈的栈顶元素相等,则最小栈栈顶元素弹出
datastack.pop();//数据栈弹出元素
}
int top() {
return datastack.top();//返回栈顶元素
}
int getMin() {
return minstack.top();//返回最小栈栈顶元素
}
};
/**
* 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();
*/
【一天一道LeetCode】#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 ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 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 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...
- Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
随机推荐
- Python Django系统
本节内容 路由系统,视图函数,模板引擎,ORM操作 FBV和CBV ORM操作补充 Cookie和Session Ajax入门 1. Django基本内容整理 1.1 路由系统 Django中路由系 ...
- Union和Union All 的区别
Union和Union All 的区别: Union 是对结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Inter ...
- ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库. 与现有的主要针对监督任务设计的深度学习库不同,ZhuSuan 的特点是深入到贝叶斯推理中,从而支持各种生成模式:传 ...
- c# 判断datatable中是否存在某列
if (datatable.columns.contains("要找的列名"))
- jquery easyui datagrid 设置设置在选中的所有行中只选择第一行
var row = $('#dg').datagrid('getSelected'); if ($('#dg').datagrid('getChecked').length > 1) { //将 ...
- 装 ubuntu + win10 出现 grub rescue 并处理之
开机出现 grub rescue 原因:装 ubuntu + win10 双系统时有可能搞坏启动文件. grub rescue 隶属于 ubuntu管理. grub rescue 里可用命令很少,主要 ...
- LINUX逻辑卷(LVM)管理与逻辑卷分区
LINUX之逻辑卷管理与逻辑卷扩展 LVM是逻辑卷管理(Logical Volume Manager)的简称,他是建立在物理存储设备之上的一个抽象层,允许你生成逻辑存储卷,和直接使用物理存储在管理上相 ...
- Ubuntu搭建owncloud10
前言: 在此我先吐槽一下.用Centos系统简直是为难我自己,是看到那个系统 感到无比的绝望. 正文: 自己在虚拟机中搭建Ubuntu系统.这里就不说了 安装好之后自己换源.建议的源: 清华源: # ...
- UI相关
前端 UI 框架 https://github.com/twbs/bootstrap https://github.com/google/material-design-lite https://gi ...
- DotnetSpider (一) 架构的理解、应用、搭建
第一次写博客,比较浅显,欢迎大牛们指点一二,不胜感激. ** 温馨提示:如需转载本文,请注明内容出处.** 本文连接:http://www.cnblogs.com/grom/p/8931650 ...