题意:实现栈的四个基本功能。要求:在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. css3文字与字体的相关样式

    给文字添加阴影——text-shadow属性 text-shadow属性是css2中定义的,在css2.1中删除了,在css3中恢复text-shadow:length length length c ...

  2. hdu4366 Successor (dfs序+zkw线段树)

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. 怎么查看linux系统是32还是64

    1.getconf LONG_BIT or getconf WORD_BIT例如:2.file /bin/ls例如: 查看linux的版本:

  4. 微信小程序运行机制

    对于扫描接口B生成的带参小程序码的问题: (1)线上版本 扫描不同带参的小程序码会重新执行小程序的整个注册程序生命周期(详细生命周期函数见官方文档), (2)扫描相同的二维码的时候,目前微信官方给出的 ...

  5. Spring入门第二十一课

    切面优先级 先看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int ...

  6. “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)

    一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增删改查的地步.测试好之后再来看这篇文章,我的主框架相对来说简答一点,重点还是实现系统的功能,以及对Jquery-Eas ...

  7. pure css做的手机页面

    <!doctype html> <html> <head> <meta http-equiv="Content-type" content ...

  8. Unity3d 5.3.5使用sqlite3

    http://blog.csdn.net/glunoy/article/details/52037598 国内讲的乱七八糟,更有故作神秘提供Mono.Data.Sqlite.dll System.Da ...

  9. Python学习笔记(正则表达式)

    \b - 表示以什么开头或结尾 \d - 匹配数字 \w - 匹配字母或数字或下划线或汉字(我试验下了,发现3.x版本可以匹配汉字,但2.x版本不可以) \s - 匹配任意的空白符 ^ - 匹配字符串 ...

  10. SonarQube总结

    官网:https://www.sonarqube.org/ 一款代码质量管理开源平台.