LeetCode(155)题解--Min Stack
https://leetcode.com/problems/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.
思路:
关键在于找最小值要O(1)复杂度,所以空间换时间。一个额外的vector存储递减序入栈的数字。
AC代码:
class MinStack {
public:
MinStack(){ st_min.push_back(INT_MAX); };
void push(int x) {
if (x <= st_min[st_min.size()-])
st_min.push_back(x);
st.push_back(x);
return;
}
void pop() {
if (st[st.size() - ] == st_min[st_min.size() - ]){
st_min.pop_back();
}
st.pop_back();
return;
}
int top() {
return st[st.size() - ];
}
int getMin() {
return st_min[st_min.size() - ];
}
private:
vector<int> st;
vector<int> st_min;
};
LeetCode(155)题解--Min Stack的更多相关文章
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- LeetCode(155) Min Stack
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- LeetCode OJ:Min Stack(最小栈问题)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- 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 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
随机推荐
- 最新版浏览器报错net::ERR_INSECURE_RESPONSE原因
访问的网址与接口请求的域名不一致,新版的chrome浏览器出于安全的考虑会将请求进行拦截,并报错net::ERR_INSECURE_RESPONSE
- Springboot 版本+ jdk 版本 + Maven 版本的匹配
Spring boot 版本 Spring Framework jdk 版本 maven 版本 1.2.0 版本之前 6 3.0 1.2.0 4.1.3+ 6 3.2+ 1.2.1 4.1.3+ 7 ...
- Linux System Programming 学习笔记(五) 进程管理
1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...
- hdu 5971 Wrestling Match 二分图染色
题目链接 题意 \(n\)人进行\(m\)场比赛,给定\(m\)场比赛的双方编号:再给定已知的为\(good\ player\)的\(x\)个人的编号,已知的为\(bad\ player\)的\(y\ ...
- checkbox 复选框只能选中一次,之后不能用
<td> <input type="checkbox" name="is_check" id="is_check" val ...
- C# Array类的浅复制Clone()与Copy()的区别
1 Array.Clone方法 命名空间:System 程序集:mscorlib 语法: public Object Clone() Array的浅表副本仅复制Array的元素,无论他们是引用类型还是 ...
- Javascript实现页面滚动时导航智能定位
遇到的问题: 在做官网的时候,需要滚动定位的区块的图片不确定,无法确定用户浏览区域对应的模块导航 之前的解决方案是: 通过定位滚动条的位置来判断用户浏览区域对应的模块导航,这种方法的弊端是,区块的高度 ...
- 51nod 1088 最长回文子串 【中心拓展法/输出长度和路径】
1088 最长回文子串 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字 ...
- springboot多环境配置
springboot多环境(dev.test.prod)配置 2017-07-17 10:33 1290人阅读 评论(0) 收藏 举报 分类: spring boot(6) 版权声明:本文为博主原 ...
- Dance In Heap(一):浅析堆的申请释放及相应保护机制
0×00 前面的话 在内存中,堆是一个很有趣的地方,因为它可以由用户去直接的进行分配与销毁,所以也产生了一些很有趣.奇思妙想的漏洞,像unlink漏洞.House系列漏洞等等.但是在学习的过程中,我们 ...