5.Min Stack(能返回最小数的栈)
Level:
Easy
题目描述:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
思路分析:
使用两个栈,一个栈压入元素,另一个栈栈顶始终保存已经入栈元素中的最小值。
代码:
class MinStack {
/** initialize your data structure here. */
Stack<Integer>pushStack=new Stack<>();
Stack<Integer>minStack=new Stack<>();
public MinStack() {
}
public void push(int x) {
pushStack.push(x);
if(minStack.isEmpty()||x<=minStack.peek()){
minStack.push(x);
}
}
public void pop() {
if(!pushStack.isEmpty()){
if((int)pushStack.peek()==(int)minStack.peek()){
minStack.pop();
}
pushStack.pop();
}
}
public int top() {
if(!pushStack.isEmpty())
return pushStack.peek();
else
return -1;
}
public int getMin() {
return minStack.peek();
}
}
5.Min Stack(能返回最小数的栈)的更多相关文章
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- 带最小值操作的栈 · Min Stack
[抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- [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] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 栈 堆 stack heap 堆内存 栈内存 内存分配中的堆和栈 掌握堆内存的权柄就是返回的指针 栈是面向线程的而堆是面向进程的。 new/delete and malloc/ free 指针与内存模型
小结: 1.栈内存 为什么快? Due to this nature, the process of storing and retrieving data from the stack is ver ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
随机推荐
- PDM中列举所有含取值范围、正则表达式约束的字段
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- 2016.9.9《Oracle查询优化改写技巧与案例》电子工业出版社一书中的技巧
1.coalesce (c1,c2,c3,c4,...) 类似于nvl但可以从多个表达式中返回第一个不是null的值 2.要在where条件中引用列的别名,可以再嵌套一层查询 select * fro ...
- 2016.1.22 利用LINQ实现DataSet内多张DataTable关联查询操作(目前未发现太大价值)
DataSet ds = new DataSet(); DataTable t1 = DBFactorySingleton.GetInstance().Factory.GetDataTable(sql ...
- suse 源的添加与删除,以及源地址
地址 一个是上海交大的,http://ftp.sjtu.edu.cn/opensuse/update/ 葡萄牙的: http://ftp.nux.ipb.pt/pub/dists/opensuse/u ...
- C语言学习笔记--函数与指针
1. 函数类型 (1)C 语言中的函数有自己特定的类型,这个类型由返回值.参数类型和参数个数共同决定.如 int add(int i,int j)的类型为 int(int,int). (2)C 语言中 ...
- C++下基本类型所占位数和取值范围
原文:http://hi.baidu.com/magicdemon/blog/item/821b2e22d7df494cad34debd.html C++下基本类型所占位数和取值范围: 符号属性 ...
- css中的hack
1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...
- 时间获取_Date\SimpleDateFormat\Calendar类
1.获取当前的日期,并把这个日期转换为指定格式的字符串,如2088-08-08 08:08:08 import java.text.SimpleDateFormat; import java.uti ...
- [转]CSS块级元素和行内元素
原地址:http://www.studyofnet.com/news/398.html 本文导读:HTML中的元素可分为两种类型:块级元素和行级元素.这些元素的类型是通过文档类型定义(DTD)来指明. ...
- wpf 窗口打开后默认设置控件焦点
https://blog.csdn.net/Vblegend_2013/article/details/81771872 <Grid FocusManager.FocusedElement=&q ...