2. Min Stack

Implement a stack with following functions:

  • push(val) push val into the stack
  • pop() pop the top element and return it
  • min() return the smallest number in the stack

All above should be in O(1) cost.

Example

Example 1:

Input:
push(1)
pop()
push(2)
push(3)
min()
push(1)
min()
Output:
1
2
1

Notice

min() will never be called when there is no number in the stack.

思路:

因为O(1),所以用两个栈操作,stack 和 minStack。

push(int num): stack push; 如果minStack为空,minStack直接push,或者 num<=minStack的栈顶元素,minStack也push

pop(): stack pop; 如果minStack的栈顶值和stack栈顶值相等(Integer类判断两个值相等要用equals.()),minStack也pop.

min(): 返回minStack 栈顶值。

注意:

(1)push的时候,num == minStack.peek() 时,也要minStack.peek()。[line 12]

否则,有可能出现EmptyStackException:第一次pop()后,minStack为空了,再min()就会有异常。

push(1)
push(1)
push(1)
min()
pop()
min()
pop()

(2)equals 和 ==

==是一个关系运算符,如果比较的两端都为基本类型,则判断两者的值是否相等,(判断过程中还有不同基本类型的转化,这里不做讨论)。如果比较的两端都为引用类型的话,则比较两者所指向对象的地址是否相同
对于equals方法,首先,能调用这个方法肯定是一个对象,然后,如果这个对象所在的类重写了equals方法,则按照重写的方法进行比较,如果没有,则比较两者所指向对象的地址是否相同。
Integer类重写了equals方法,所以equals方法才是比较两个值是否相等,而不能用==(== 用来比较两个引用变量存储的地址是否相同,也就是是否指向同一个地址)
 
代码:
 public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack; public MinStack() {
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
} public void push(int number) {
stack.push(number);
if (minStack.empty() || minStack.peek() >= number) { //相等时,minStack也要push
minStack.push(number);
}
} public int pop() {
if (stack.peek().equals(minStack.peek())) //比较栈顶值,只能用equals()
minStack.pop();
return stack.pop();
} public int min() {
return minStack.peek();
}
}

Lintcode12-Min Stack-Easy的更多相关文章

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

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

  2. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  3. [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 ...

  4. leetcode 155. Min Stack --------- java

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

  5. Min Stack [LeetCode 155]

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

  6. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  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解决共享

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

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

随机推荐

  1. OJ#1002 又是a+b

    题目描述: 给定两个正整数a.b(0 < a,b < =10000),求出a+b的和 输入描述: 多组输入,每组输入为一行,里面有2个数a,b(0 < a,b < =10000 ...

  2. 前端 html 篇

    1 link和@import的区别是?     区别1:link是XHTML标签,除了加载CSS外,还可以定义RSS等其他事务:@import属于CSS范畴,只能加载CSS. 区别2:link引用CS ...

  3. Eclipse 00: 安装教程 + 汉化 + 简单创建java项目

    Java 安装教程(Eclipse) 目录: 要安装Java 要分两个步骤: 1.JDK的安装 2.Eclipse的安装 3.Eclipse汉化 4.Eclipse创建简单java项目 1和2的顺序不 ...

  4. python2,python3同时安装时,python3可以安装并升级pip库,python2报错的解决办法

    最近在使用pip安装包的的时候出现下面错误 UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' 查询资料后发现原因是p ...

  5. pycharm import pygame 出现报错:No module named 'pygame'

    首先发现装的Python 有问题原来的Python3.6.4版本安装完成后Scripts文件夹里空白的,什么也没有,从https://www.python.org/downloads/windows/ ...

  6. toolbar按钮添加图标

    需要toolbar关联imagelist组件,imagelist组件添加需要的图片,在toolbar新建按钮,按钮中选择相应图表.

  7. MySql查看与修改auto_increment方法(转)

    add by zhj:  在创建表时,如果没有显式的指定AUTO_INCREMENT的值,那它默认是1 原文:https://blog.csdn.net/fdipzone/article/detail ...

  8. keras实现textcnn

    https://github.com/MoyanZitto/keras-cn/blob/master/docs/legacy/blog/word_embedding.md 这个链接将带有embedin ...

  9. python练习题-day11

    1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...

  10. PyQt5简介及demo

    PyQt5说明 pyqt5是一套Python绑定Digia QT5应用的框架.它可用于Python 2和3.本教程使用Python 3.Qt库是最强大的GUI库之一.pyqt5的官方网站http:// ...