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/ 题目描述: ...
随机推荐
- 用random模块实现验证码
#! /usr/bin/env python3 import random checkcode = "" ## 全部为数字的验证码 # for i in range(4): # c ...
- 20145322《Java程序设计》第5次实验报告
20145322<Java程序设计>第5次实验报告 实验内容 1.根据所学内容,编写代码实现服务器与客户端 2.掌握密码技术的使用 3.设计安全传输系统,客户端中输入明文,利用DES算法加 ...
- 20145328 《Java程序设计》实验五实验报告
20145328 <Java程序设计>实验五实验报告 实验名称 Java网络编程 实验内容 用书上的TCP代码,实现服务器与客户端. 客户端与服务器连接 客户端中输入明文,利用DES算法加 ...
- 20145240《网络对抗》MSF基础应用
MSF基础应用 一个主动攻击,如ms08_067 启动msf search ms08_067,查找相应的漏洞,查询可攻击的模块. 根据上述漏洞的模块use exploit/windows/smb/ms ...
- Shell学习小结 - 深入认识变量
移动端访问不佳,请访问我的个人博客 变量的命名 对于初学者来说,可以简单的理解为,变量就是保存在计算机内存中的一系列的键值对. 列如: str="hello" 这里的str就是变量 ...
- tornado之WebSocket
WebSocket WebSocket是HTML5规范中新提出的客户端-服务器通讯协议,协议本身使用新的ws://URL格式. WebSocket 是独立的.创建在 TCP 上的协议,和 HTTP 的 ...
- 第十篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 query
/** Spark SQL源码分析系列文章*/ 前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的. 那么基于以上存储结构,我们查询cache在 ...
- swoole http_server 多进程并使用多进程处理消息
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set([ 'worker_num' =&g ...
- 自学Jav测试代码三 Math类 & Date & GregorianCalendar类
2017-08-23 20:30:08 writer: pprp package test; import java.util.Date; import java.util.*; public cla ...
- RabbitMQ 之 订阅模式 Publish/Subscribe
模型图 我们之前学习的都是一个消息只能被一个消费者消费,那么如果我想发一个消息 能被多个消费者消费,这时候怎么办? 这时候我们就得用到了消息中的发布订阅模型 在前面的教程中,我们创建了一个工作队列,都 ...