Lintcode 带最小值的栈
来源:互联网 发布:最好广告过滤软件 编辑:程序博客网 时间:2024/06/06 07:42
带最小值操作的栈
描述
笔记
数据
评测
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。
你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。
您在真实的面试中是否遇到过这个题? Yes
注意事项
如果堆栈中没有数字则不能进行min方法的调用
样例
如下操作:push(1),pop(),push(2),push(3),min(), push(1),min() 返回 1,2,1
要注意的是,返回了当前栈中最小值后,还要能继续返回次最小值。所以不能只用一个变量标记最小值。
class MinStack {public: stack<int> s_num; stack<int> s_min; MinStack() { // do initialization if necessary } void push(int number) { // write your code here this->s_num.push(number); if(this->s_min.empty()){ this->s_min.push(number); }else{ int tmp = (number<this->s_min.top())?number:this->s_min.top(); this->s_min.push(tmp); } } int pop() { // write your code here int tmp = this->s_num.top(); this->s_num.pop(); this->s_min.pop(); return tmp; } int min() { // write your code here if(this->s_min.empty()&&this->s_num.empty()){ return -1; } return this->s_min.top(); }};
0 0
- Lintcode 带最小值的栈
- LintCode 带最小值操作的栈
- lintcode-带最小值操作的栈-12
- LintCode:带最小值操作的栈
- lintcode,带最小值操作的栈
- lintcode,带最小值操作的栈
- LintCode 12 带最小值操作的栈
- lintcode -- 带最小值操作的栈
- [LintCode]12.带最小值操作的栈
- LintCode-----12.带最小值操作的栈
- lintcode带最小值操作的栈
- Lintcode 12. 带最小值操作的栈
- LintCode-剑指Offer-(12)带最小值操作的栈
- [LintCode] 带最小值操作的栈 Min Stack
- [未解决][lintcode]带最小值操作的栈
- Google/LintCode:M-带最小值操作的栈
- (lintcode)第12题带最小值操作的栈
- [LintCode 12] 带最小值操作的栈(Python)
- 如何删除流氓 ?搜狗网址导航
- 杭电1556 Color the ball
- svn 创建用户账号
- 中文乱码解决
- 关于过某P的双机联调
- Lintcode 带最小值的栈
- [Robotics: Aerial Robotics] Quadrotor Dynamics 四旋翼无人机动力学
- 调整数组顺序使奇数位于偶数前面
- x265探索与研究(九):compressFrame()函数
- HDU1084 What Is Your Grade?
- 168.leetcode Excel Sheet Column Title(easy)[进制转换]
- HDU 1098 Ignatius's puzzle(数学归纳题or费马小定理)
- linux内核的make modules 有什么用
- jquery table表格 获取选中的某一行和某一列的值