leetCode(45):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) {
data.push(x);//数据栈正常压入
if(minData.empty())
{//最小栈数据栈压入时要进行推断。假设被压入的数据不影响最小值,则直接再压入
//最小栈栈顶元素,否则压入x
minData.push(x);
}
else
{
if(x<minData.top())
{
minData.push(x);
}
else
{
minData.push(minData.top());
}
}
} void pop() {//弹出时,两个栈都须要弹出
data.pop();
minData.pop();
} int top() {
return data.top();
} int getMin() {
return minData.top();
} private:
stack<int> data;
stack<int> minData;
};
leetCode(45):Min Stack的更多相关文章
- 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 、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(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- 微信小程序radio组件 - 如何改变默认样式大小?
今天在写小程序的时候用到radio组件,但是很懊恼并未提供修改radio组件大小属性,第一感觉准备用css width , height 改变radio的大小,但是怎么搞也无法改变. 但是又不愿意搞个 ...
- 玩转树莓派 - 修改Raspbian软件源加快软件下载速度
这是 meelo 原创的 玩转树莓派 系列文章 步骤1:登录到Raspbian的命令行界面 步骤2:修改Raspbian的软件源 软件源是Linux系统免费的应用程序安装仓库,很多的应用软件都会这收录 ...
- AC日记——自然数和分解 codevs 2549
自然数和分解 思路: 水题: 代码: #include <bits/stdc++.h> using namespace std; ][]; int main() { cin>> ...
- hdu 5117 数学公式展开 + dp
题目大意:有n个灯泡,m个按钮,(1 <= n, m <= 50),每个按钮和ki 个灯泡相关, 按下后,转换这些灯泡的状态,问你所有2^m的按下按钮的 组合中亮着的灯泡的数量的三次方的和 ...
- tmpfs文件系统
centos 7测试OK 创建挂载点,挂载 mkdir -p /run/testdirmount -nt tmpfs -o size=500m,mode=755 tmpfs /run/testdir
- 日志 log4net
先引入log4net 接着配置configuration文件 <?xml version="1.0"?><configuration> <system ...
- es2015(es6)学习总结
1.三种声明方式 var:它是variable的简写,可以理解成变量的意思. let:它在英文中是“让”的意思,也可以理解为一种声明的意思. const:它在英文中也是常量的意思,在ES6也是用来声明 ...
- str 编码
你需要的是让编码用实际编码而不是 ascii 1 对需要 str->unicode 的代码,可以在前边写上 import sys reload(sys) sys.setdefault ...
- Java工具类-验证码工具
1.工具类,生成随机验证码字符串 import java.util.Arrays; /** * 工具类,生成随机验证码字符串 * * @version 1.0 * @author * */ publi ...
- Linux前后台进程切换
(1).Linux前台进程与后台进程的区别 前台进程:是在终端中运行的命令,那么该终端就为进程的控制终端,一旦这个终端关闭,这个进程也随之消失. 后台进程:也叫守护进程(Daemon),是运行在后台的 ...