题意:实现栈的四个基本功能。要求:在get最小元素值时,复杂度O(1)。

思路:链表直接实现。最快竟然还要61ms,醉了。

 class MinStack {
public:
MinStack(){
head.next=;
head.t=;
m=0x7FFFFFFF;
}
void push(int x) {
node *p=(node *)new(node);
p->t=x;
p->next=head.next;
head.next=p;
head.t++;
if(x < m) //要更新
m = x;
} void pop() { if(==head.t) return ;
else if(head.t)
{
head.t--;
if(head.next->t==m) //刚好等于最小值m,要更新最小值
{
int sma=0x7FFFFFFF;
node *p=head.next->next;
while( p )
{
if(p->t<sma)
sma=p->t;
p=p->next;
}
m=sma;
}
head.next=head.next->next;
}
} int top() {
if(head.t)
return head.next->t;
return ;
} int getMin() {
return m;
}
private:
struct node
{
int t;
node *next;
};
node head;
int m;
};

LeetCode Min Stack 最小值栈的更多相关文章

  1. [LeetCode] Min Stack 最小栈

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

  2. [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 ...

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

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

  4. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  5. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  6. [LeetCode] Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  7. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  8. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  9. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

随机推荐

  1. windows7如何打开远程桌面&nbsp;-…

    单位的机器,刚装上了windows7旗舰版(当然不是花银子滴),想打开远程桌面连接,这样从别的机器登录也方便.可是问题来了,windows7对安全的设置比较高,不像windows XP那么随便一点就可 ...

  2. Eclipse&nbsp;安装插件

    Eclipse 安装插件 本文介绍Eclipse插件的安装方法.Eclipse插件的安装方法大体有三种:直接复制.使用link文件,以及使用eclipse自带的图形界面的插件安装方法. AD: 做为当 ...

  3. Python开发【第四篇】:运算符

      1. 算术运算符   算术运算符包括+.-.*./.%.//.**,即加减乘除,取余,取商(地板除法)以及幂运算. >>> 5 + 2 7 >>> 5 - 2 ...

  4. WPF语言切换,国际化

    winform语言切换在每个窗口下面有一个.resx结尾的资源文件,在上面添加新字符串就好了: WPF语言切换跟winform不一样的地方在于需要自己添加资源文件,并且这个资源文件可以写一个,也可以写 ...

  5. mysql由浅入深探究(四)----mysql事务详解

    什么是事务: 通俗的解释就是对数据库进行的一组完整的操作,这组完整的操作中包含一个或多个操作.解释的太low了,来点官方的:事务就是DBMS中执行的一个完整的逻辑单元,这个逻辑单元中包含一个或者多个操 ...

  6. UIPI VS与Win7 共舞:用户界面特权隔离

    http://tech.it168.com/a2009/0924/737/000000737968.shtml [IT168 专稿]在上文中,我们介绍了操作系统服务的Session 0隔离,通过Ses ...

  7. uoj#399. 【CTSC2018】假面(概率期望)

    传送门 记\(p_{i,j}\)为\(i\)还剩\(j\)滴血的概率,那么\(i\)最后血量的期望就是\[E_i=\sum_{j=0}^{m_i}j\times p_{i,j}\] 然后\(p\)数组 ...

  8. jquery 插件的实现和优化

    1.menus 实现: $.fn.menu=function(options){ var $this=$(this); var cross='<div class="zhiniu_cr ...

  9. windows installer cleanup utility - Windows下卸载神器

    https://windows-installer-cleanup-utility.soft32.com

  10. JS 检测字符串是否还有某个字符

    function filer(s) { var str = "字符串"; if (str.indexOf(s) == -1) { alert("没有"); } ...