【一天一道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 ...
随机推荐
- Ubuntu 16.04 Vim安装及配置
安装VIM 默认已经安装了VIM-tiny acewu@acewu-computer:~$ locate vi | grep 'vi$' |xargs ls -al lrwxrwxrwx 1 root ...
- 【Git】CentOS7 通过源码安装Git
yum源仓库里的Git版本更新不及时,最新版的Git是1.8.3,但是官方的最新版早已经更新到2.9.5.想要安装最新版本Git,只能下载源码进行安装 建议最好更新git为较新版本,便于使用 1.查看 ...
- Angular 和 Vue 使用的对比总结 -- 脚手架
前言 之前是用Vue的,现在由于工作原因,开始使用Angular.分别是Vue2和Angular5入的坑.只是从使用上来对比总结,加深记忆,避免混淆. 什么 ? 你问实现原理的异同及优劣? 本宝宝还 ...
- Mysql锁机制--并发事务带来的更新丢失问题
Mysql 系列文章主页 =============== 刚开始学习 Mysql 锁的时候,觉得 Mysql 使用的是行锁,再加上其默认的可重复读的隔离级别,那就应该能够自动解决并发事务更新的问题.可 ...
- js修改伪类元素样式
<style type="text/css"> .htmlbox_close::before, .htmlbox_close::after { content: ''; ...
- DecimalFormat(用于格式化十进制数字)
1概念: DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字.该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言.阿拉伯语和印度语数 ...
- Vue-起步篇:Vue与React、 Angular的区别
毋庸置疑,Vue.React. Angular这三个是现在比较火的前端框架.这几个框架都各有所长,选择学习哪种就得看个人喜好或者实际项目了.相比之下, Vue 是轻量级且容易学习掌握的. 1.Vue和 ...
- SQL部分常用指令整理
dual 伪表 用来测试函数和表达式 1.查询EMP表中所有人的信息,结果格式样例为"某人的月薪是1000$" SELECT ENAME||'的月薪是'||SAL||'$' FRO ...
- k8s Kubernetes v1.10
#转移页面 http://www.cnblogs.com/elvi/p/8976305.html
- How To Automate Disconnection of Idle Sessions
***Checked for relevance on 30-Apr-2012*** goal: How to automate disconnection of idle sessions fact ...