Lintcode12-Min Stack-Easy
2. Min Stack
Implement a stack with following functions:
push(val)
push val into the stackpop()
pop the top element and return itmin()
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 和 ==
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的更多相关文章
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- 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 ...
随机推荐
- vue中的图标字体引入
网址:https://icomoon.io/app/#/select: 特点:样式多,免费 操作: 1.相中的,随便点,不要钱,generat fonts然后download,得到一个压缩文件,解压, ...
- mui 轮播
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- windows MYSQL 安装及修改root密码
官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) 我放在了这里 E ...
- Java Filter(拦截器)
多个Filter按照在配置文件中配置的filter顺序执行. 在web.xml文件中配置该Filter,使用init-param元素为该Filter配置参数,init-param可接受如下两个子元素: ...
- mdf ldf添加到数据库
1.拷贝mdf ldf文件到某个文件夹下 2.打开SQL执行语句: USE master; GO CREATE DATABASE NewFile ON (FILENAME = 'C:\Program ...
- Jenkins在windows服务器上依赖的maven仓库目录
1.在windows server 2008上,maven仓库路径为: C:\Users\用户名\.m2 2.在windows server 2003上(加入域的服务器),maven仓库路径为: C: ...
- JavaWeb & Tomcat
1 JavaWeb概述 Java在服务器端的应用有Servlet,JSP和第三方框架等. Java的Web框架基本都遵循特定的路数:使用Servlet或者Filter拦截请求,使用MVC的思想设计架构 ...
- SpringMVC整合mybatis基于纯注解配置
Mybatis整合Spring配置 第一部分:配置Spring框架 配置SpringMVC的步骤 配置流程图 导入包(哪些包,基本包5个,1日志依赖包,2webmvc支持包)SpringMVC配置 & ...
- mescroll在vue中的应用
1.npm install --save mescroll.js 2. <template> <div> <!--全部--> <mescroll-vue re ...
- 小程序图表wx-chart
下载地址 https://github.com/xiaolin3303/wx-charts 使用步骤: 一.解压后,把dist里面的wxcharts.js或者wxcharts-min.js放在小程序的 ...