题意:设计一个能输出栈内最小值的栈

该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm

在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x

同样地在pop时,s的元素被删除了,那么sm中的也应该被删除。

通过这些操作维护sm能很巧妙在O(1)复杂度得到最小值。

 class MinStack {
public:
stack<int> sm;
stack<int> s;
void push(int x) {
s.push(x);
if(sm.empty() || (!sm.empty() && sm.top() >= x)) sm.push(x);
} void pop() {
if(s.top() == sm.top()) sm.pop();
s.pop();
} int top() {
return s.top();
} int getMin() {
return sm.top();
}
};

Leetcode 155 Min Stack的更多相关文章

  1. 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 ...

  2. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  4. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. Java for LeetCode 155 Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  7. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  8. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  9. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

随机推荐

  1. 获取datagrid选择行

    var rows = $('#dg').datagrid('getChecked');     var ids = '';  for (var i = 0; i < rows.length; i ...

  2. each处理json数据

    eg:给传进来的ID中当其对应的值为true时,即给对应的ID标签添加一个class 名为  focus,如: var obj = { id01:'true', id02:'flase', id03: ...

  3. linux 下两台电脑之间ssh无密码连接

    例子:在192.168.0.12使用tecmint用户,连接192.168.0.11主机上的sheena用户 Step 1: Create Authentication SSH-Kegen Keys ...

  4. [MOSEK] Mosek求解中遇到的奇葩内存问题

    在使用mosek优化库的时候,使用http://docs.mosek.com/7.0/capi/MSK_getxx_.html的 MSKrescodee MSK_getxx ( MSKtask_t t ...

  5. iOS从不同页面跳转回到指定控制器

    HomeViewController *homeVC = [[HomeViewController alloc] init]; UIViewController *target = nil; for ...

  6. 日志log使用序列反序列加密(Serializer) DESCrypto 加密

    若一次加密一个文件内容,文件内容不会更新变化,网上大多数序列化反序列加密程序是没问题的. 1:由于log文件的随时会更新内容,那网上常用的程序是行不通的.需要做修改 若想通过打开txt , using ...

  7. 阿牛OCX编程助手

    ※◆☆★☆◆※欢迎使用阿牛OCX编程助手,此程序为按键精灵专用,如终请联系作者QQ:82850696*0*测试版已停用*0*2014-12-27 14:05:59*哈密*E2873D0137C6D04 ...

  8. 关于VMWARE 上对于不小心VMWare Tools Easy Install 后卡死的解决方法

    PS 要想修改首先开机的时候会进入命令行,输入用户名,密码后,可以用startx来进入图形界面(也有人将/etc/init.d/ligtmd start可以懂事本人那次是不行的) 通常网上人们的解决办 ...

  9. Apache 常用伪静态配置

    1. /a/b?c=d => index.php?_a=a&_m=b&c=d 2. /xxx/detail-yyy.html => index.php?_a=xxx& ...

  10. Linux下运行jar包

    方法① 1.vim xxx.jar 2.配置程序入口:找到MANIFEST.MF,添加Main-Class:+空格+package.class 3.引入第三方jar包:①在MANIFEST.MF中加入 ...