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. linux 中的进程wait()和waitpid函数,僵尸进程详解,以及利用这两个函数解决进程同步问题

    转载自:http://blog.sina.com.cn/s/blog_7776b9d3010144f9.html 在UNIX 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / wait ...

  2. libtommath.a: could not read symbols: Bad value 编译错误

    最近做个项目需要RSA,便调用了tommath,平时开发环境都在32位的系统上,编译运行一切都没问题,但当把程序换到一台64位系统上编译时出现: /usr/bin/ld: /usr/lib/gcc/x ...

  3. JS-easyui 扩展easyui.datagrid,添加数据loading遮罩效果代码

    (function (){ $.extend($.fn.datagrid.methods, { //显示遮罩 loading: function(jq){ return jq.each(functio ...

  4. [Spring] Java spring quartz 定时任务

    首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath ...

  5. Qt Q_DECLARE_METATYPE说明——让自定义的类型设置到QVariant

    在使用Qt进行应用程序开发时,经常要自定义数据类型,而且在需要的时候还要把这些数据放到QVariant中去.因为QVariant是一个接受绝大部分类型的数据类型.为了达到这个目的,就必须使用Q_DEC ...

  6. 【采集层】Kafka 与 Flume 如何选择

    转自:http://my.oschina.net/frankwu/blog/355298 采集层 主要可以使用Flume, Kafka两种技术. Flume:Flume 是管道流方式,提供了很多的默认 ...

  7. imx6 fec分析

    /***************************************************************************** * imx6 fec分析 * 本文主要分析 ...

  8. 编译 boost 库(win7+boost1.60+vs2008)

    参见:http://blog.csdn.net/u013074465/article/details/42532527 下载boost安装包 https://sourceforge.net/proje ...

  9. Servlet程序的入口点是?( )

    Servlet程序的入口点是?( ) A.init() B.main() C.service() D.doGet() 解答:C

  10. 谷歌修复了 FFmpeg 中上千个 bug

    谷歌在科技业界中几乎每天都会创造出新闻素材,它的触手涉及到了生活中的多个领域.最近谷歌将其Google +社交网络与邮件服务Gmail相结合.然而今天谷歌宣布他们修复了FFmpeg的上千个bug.   ...