写一个栈,支持push pop top getMin

难就难在在要在常量时间内返回最小的元素。

一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了。

后来想到其实用一个栈来记录当前的最小值就好了,只有当被删除的元素等于min栈的栈顶元素时,才删除min栈里面的元素。

min栈中每个元素代表着那一段栈中的最小值了。

// stack: store the stack numbers
private Stack<Integer> stack = new Stack<Integer>();
// minStack: store the current min values
private Stack<Integer> minStack = new Stack<Integer>();

public void push(int x) {
// store current min value into minStack
if (minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
}

public void pop() {
// use equals to compare the value of two object, if equal, pop both of them
if (stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
}

public int top() {
return stack.peek();
}

public int getMin() {
return minStack.peek();
}

LeetCode题解 #155 Min Stack的更多相关文章

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

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

  2. 【leetcode】155 - Min Stack

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

  3. 【一天一道LeetCode】#155. Min Stack

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Design ...

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

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

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

  6. leetcode 155. Min Stack --------- java

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

  7. Java [Leetcode 155]Min Stack

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

  8. 155. Min Stack

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

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

随机推荐

  1. ViewPager实现图片轮翻效果

    很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的.今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的 ...

  2. 说说geotools中坐标转换那点事

    概述: 本文说说geotools中坐标转换的那点事情,以WGS84和web墨卡托相互转换为例. 效果: 转换前 转换后 单个Geometry转换 实现代码: package com.lzugis.ge ...

  3. PHP内核研究

    深入理解PHP内核:Think In PHP Internals(TIPI)是一个开源项目 ,分享PHP内部实现的细节,如内核,扩展等.官网见:http://www.php-internals.com ...

  4. RedHat Server Enterprise 6安装G++

    RedHat 6默认是安装有GCC,而没有安装G++编译 要安装G++前最好先查看下GCC的版本号,通常GCC的版本和G++的版本是相同的,知道GCC的版本再去找G++的安装文件就容易些,版本号有在安 ...

  5. Android的长度单位及屏幕分辨率

    屏幕分辨率基础 1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸 摩托罗拉milestone手机 ...

  6. js实现tab页面不同内容切换显示

    效果      实现的思路如下: controller层同时把两个内容都查处理 前端html用js控制显示 (1)前端的tab代码 (2)tab内容的结构 (3)关键部分 js $(".hd ...

  7. wifi文件传输

    步骤: 1.下载CocoaHTTPServer 2.解压后,将CocoaHTTPServer-master目录下的Core导入工程. 3.打开Samples/SimpleFileUploadServe ...

  8. test20181219 连续段的期望

    题意 连续段的期望 [问题描述] 小N最近学习了位运算,她发现2个数xor之后数的大小可能变大也可能变小,and之后都不会变大,or之后不会变小.于是她想算出以下的期望值:现在有 N个数排成一排,如果 ...

  9. IBM Cognos 10.2 最新体验之旅

    IBM Cognos Data Manager 数据集市的构建利器 本文详细的介绍了 Cognos 最新版本 10.2 的数据集市构建器 Data Manager 的使用,对于希望系统了解 Cogno ...

  10. Weblogic启动成功,控制台打不开

    有时候,我们在linux操作系统上成功启动了weblogic,也查看了7001端口的状态是开启的.但是访问weblogic控制台没有反应,也没有报错. 使用 netstat -ano | grep 7 ...