写一个栈,支持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. mongodb倒排索引

    这周主要都花时间搞mongodb上了,业务场景是上游产出几个城市的全量道路code值,每个城市的数据量大概在100w~200w之间,每条数据对应好几个feature,形如: { code: 0, fe ...

  2. windows下memcached安装以及php_memcache.dll扩展

    http://kimi.it/258.html http://kimi.it/259.html https://www.cnblogs.com/elenaPeng/p/6877530.html htt ...

  3. react style: 二级菜单

    1.样式 @import "../../styles/varibles"; .app-sidebar { overflow: hidden; width: 180px; > ...

  4. 【python】imp模块的使用

    是import在程序中的使用 [一]函数load_source imp.load_source(moduleName, sourceFile) 使用: abc = imp.load_source('d ...

  5. canvas - 圆圈内 hover 高亮 效果

    各种计算还挺繁琐的, 关键点在角度的计算, 根据鼠标位置, 利用atan(y/x) 得到反正切值 , 角度  (tan输入的是r和x围成的那个角,输出的是y/x.反tan就是输入y/x输出角.) &l ...

  6. 任务调度 Spring Task 4(二 )

    注解和配置文件两种 第一种:配置文件方式 第一步:编写作业类 即普通的pojo,如下: import org.springframework.stereotype.Service; @Service ...

  7. canvas 绘制文本

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. I.MX6 change boot partition 1 to User area

    /************************************************************************************ * I.MX6 change ...

  9. 在Mac中安装python,配置python环境

    参考链接:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 其实跟windows差不多,就是在python官网下载特定版本的python ...

  10. 十、python沉淀之路--高阶函数初识

    一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...