LeetCode OJ: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 {
public:
void push(int x)
{
s.push(x);
if(sMin.empty() || x <= sMin.top())
sMin.push(x);
}
void pop()
{
if(s.top() == sMin.top()){
sMin.pop();
}
s.pop();
}
int top()
{
return s.top();
}
int getMin()
{
return sMin.top();
}
private:
stack<int> s;
stack<int> sMin;
};
LeetCode OJ:Min Stack(最小栈问题)的更多相关文章
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- $如何用Python装饰器实现一个代码计时器?
有时候我们很希望看到程序中某个函数或某个代码段的耗时情况,那么该如何办呢?本文用两种方式实现了代码计时器的功能,第一种方式是采用装饰器来实现,第二种方式采用上下文管理器实现. 其实计算代码的运行时间, ...
- win7 eclipse设置Courier New字体
win7系统 1.控制面板-->字体.找到Courier New 字体,右键->显示,这种字体就开始变亮了. 2.eclipse里设置: windows-->Preferences- ...
- vue下载文件
import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...
- php 安装Memcache扩展
转载地址:http://www.tuicool.com/articles/EB3imm 文章概述:由于当前机器安装的php,是用yum安装,现在需要使用到memadmin做一些监控, memadmin ...
- mouseleave mouseout时候悬浮框不应该消失的时候消失了 css 解决办法
要实现的效果和代码思路 简单来说就是 用一个div包着喇叭和悬浮框 悬浮事件写在这个div上 鼠标悬浮到div上的时候 悬浮框出现 最终要做成鼠标从小喇叭移动到下面的框上的时候 下面框是不会消失的. ...
- 20145321 《Java程序设计》第5周学习总结
20145321 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 1.Try.catch:Java中所有错误都会被打包为对象,通过try和catch语法可以对代表错误的对象做 ...
- PayPal2019春招实习生笔试题的某一题
题目简单描述:给你n个点的坐标(x, y),均为浮点数. 如果任意两个点之间的欧几里得距离小于给定的一个浮点值,则认为这两个点之间有关联,并且关联具有传递性,总之就是尽可能扩大一个集合. 输入: d ...
- 使用Spring注解注入属性
本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...
- 复习指南(Pascal版)
[第一层级 条件反射] 1.个十百千各数位的求法 q:=a div 1000 mod 10; b:=a div 100 mod 10; s:=a div 10 mod 10; g:=a mod 10; ...
- tcpdump抓包笔记
抓取指定端口的数据包 并保存文件,用wireshark分析 tcpdump -Ans 4096 -i any port 8080 -w ../mpass.cap 抓取指定端口和指定ip的数据包 并保存 ...