Description:

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.

带有minimum element属性的stack操作。

class MinStack {
public Node node = null;
public void push(int x) {
if(node == null) {
node = new Node(x);
node.min = x;
}
else {
Node tNode = new Node(x);
tNode.next = node;
node = tNode;
node.min = node.next.min < x ? node.next.min : x;
}
} public void pop() {
node = node.next;
} public int top() {
return node.val;
} public int getMin() {
return node.min;
}
}
class Node {
int val;
int min;
Node next;
public Node(int val) {
this.val = val;
} }

好久没1A了。

LeetCode——Min Stack的更多相关文章

  1. leetCode Min Stack解决共享

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

  2. LeetCode: Min Stack 解题报告

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

  3. [LeetCode] Min Stack 最小栈

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

  4. [LeetCode] Min Stack

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

  5. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  6. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  7. [LeetCode] Min Stack 栈

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

  8. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  9. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

随机推荐

  1. 构建基于分布式SOA架构的统一身份认证体系

    摘要:本文充分利用SOA架构松耦合的特点,通过规范统一网络接口实现业务系统整合,既提升系统安全性,又简化资源访问操作,具有重要的理论和现实意义. 统一身份认证旨在将分散在各个信息系统中的用户和权限资源 ...

  2. 我的mac的其他满了,发现是一个叫core的文件

    6Q12KFX%PDARS7B{B__OZVW.jpg (97.93 KB, 下载次数: 0) 下载附件  保存到相册 2014-7-3 15:40 上传   如图,电脑虽然买了半年但是基本没怎么用, ...

  3. mac配置完ssh依然提示"Enter passphrase for key"解决方法

    使用Git,每次都提示输入命令. 这个问题折磨很久,明明已经配置过ssh了,可是每次还要提示输入密码,从网上查查,最后一条命令解决问题: 问题提示: Enter passphrase for key ...

  4. 下载安装 Apache(Windows 64位)

    32位的Apache的下载安装:http://jingyan.baidu.com/album/2f9b480dae458f41cb6cc2ce.html?picindex=2 64位的Apache的下 ...

  5. C++/C语言的标准库函数与运算符的区别new/delete malloc/free

    malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++ ...

  6. 关于Cocos2d-x的数据存储

    Cocos2d-x对数据的存储没有用到数据库,但是有用到一个类似数据库的小型数据库,就是数据存储.存储后的数据用XML的文件格式保存在C:\Users\Administrator\AppData\Lo ...

  7. ftp传输出现问题

    将文件打包压缩之后,从ftp服务器下载到本地,进行解压,出现下面的问题. 原来是传输文件的时候是默认按照netascii的格式进行传输的,没有按照二进制文件的形式传输. 再传输之前,使用bin指定传输 ...

  8. Mysql数据库中InnoDB和MyISAM的差别

    Mysql数据库中InnoDB和MyISAM的差别 InnoDB和MyISAM是在使用MySQL最常用的两个表类型,各有优缺点,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理, ...

  9. (38)JS运动之淡入淡出

    基本思路:使用样式filter.可是要区分IE浏览器和chrom.firefox的不同,详细也是用定时器来实现. <!DOCTYPE HTML> <!-- --> <ht ...

  10. OpenGL模板缓冲区与模板测试

    原文地址:http://www.blogjava.net/qileilove/archive/2014/01/23/409269.html 帧缓冲区有许多缓冲区构成,这些缓冲区大致分为: 颜色缓冲区: ...