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. 剑指offer——python【第31题】整数1出现的次数

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  2. Python学习之旅(十二)

    Python基础知识(11):高级特性 一.分片(切片) 通过索引来获取一定范围内的元素 #字符串 s="Alice" s[0:4:2] 结果: 'Ai' #列表 l=[1,2,3 ...

  3. ERP实施顾问--理解客户的解决方案与实际需求

    在企业进行信息化时实施方的顾问都会来现场进行"需求调研",再根据"调研"的结果进行双方确认,确认后按此蓝本进行开发实施. 一切看上去都很美好,需求明确.开发顺利 ...

  4. Codeforces 677 - A/B/C/D/E - (Undone)

    链接: A - Vanya and Fence - [水] AC代码: #include<bits/stdc++.h> using namespace std; ; int n,h; in ...

  5. mysql 用户及权限

    永远不要给任何人(除了MySQL root帐户)访问 数据库中的 user表mysql!这很关键. mysql.user表存放所有用户的主机,用户名,密码,权限.直接修改表中信息,需flush pri ...

  6. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  7. 11.1-uC/OS-III就绪列表

    准备好运行的任务被放到就绪列表中, 如图6-1.就绪列表是一个数组( OSRdyList[]),它一共有OS_CFG_PRIO_MAX条记录,记录的数据类型为OS_RDY_LIST(见OS.H).就绪 ...

  8. CS1704问题汇总

    “/”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS1704: 已经导入了具有相同的 ...

  9. MySQL的分区、分表、集群

    1.分区 mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表结构的,一 ...

  10. 【UML】NO.53.EBook.6.UML.2.001-【Thinking In UML 大象 第二版】- 概述

    1.0.0 Summary Tittle:[UML]NO.53.EBook.6.UML.2.001-[Thinking In UML 大象 第二版]- 概述 Style:DesignPattern S ...