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. Could not find or load main class org.apache.spark.deploy.yarn.ApplicationMaster

    Spark YARN Cluster mode get this error "Could not find or load main class org.apache.spark.depl ...

  2. 项目实战02:nginx 反向代理负载均衡、动静分离和缓存的实现

    目录 实验一:实现反向代理负载均衡且动静分离 1.环境准备: 2.下载编译安装tengine 3.设置代理服务器的配置文件 4.启动tengine服务 5.开启后端的web服务 6.测试 实验二:ng ...

  3. scala-协变和逆变

    class GaoJi class ZhongJi extends GaoJi //协变=========================== class Card[+T] val cgaoji = ...

  4. RoR - MetaProgramming

    ruby是动态语言,它有动态语言的优势与劣势 动态语言,像python与ruby 你不用提前去定义method - they need to only be "found" whe ...

  5. Elasticsearch学习笔记(六)核心概念和分片shard机制

    一.核心概念 1.近实时(Near Realtime NRT) (1)从写入数据到数据可以被搜索到有一个小延迟(大概1秒): (2)基于es执行搜索和分析可以达到秒级 2.集群(Cluster) 一个 ...

  6. Linux下Solr单机版、集群版安装与配置

    一.安装 1.需要的安装包有apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 这里默认大家已经安装好jdk与tomcat,所以在这里不做 ...

  7. 14.0-uC/OS-III挂起队列

    1.当任务等待信号量. mutex.事件标志组.消息队列时,该任务会被放入挂起队列. 挂起队列是一个OS_PEND_LIST类型的数据结构,它包含了三部分内容. .NbrEntries 挂起队列中有几 ...

  8. REST风格的5条关键原则

    REST风格的5条关键原则包括: (1)网络上的所有事物都被抽象为资源. (2)每个资源对应一个唯一的资源标识. (3)通过通用的连接件接口对资源进行操作. (4)对资源的各种操作不会改变资源标识. ...

  9. Redis入门到高可用(十五)—— HyperLogLog

    一.简介 二.API Demo 三.使用经验

  10. python框架之Django(14)-rest_framework模块

    APIView django原生View post请求 from django.shortcuts import render, HttpResponse from django import vie ...