155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈。
push(x) -- 将元素x推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
详见:https://leetcode.com/problems/min-stack/description/
Java实现:
方法一:
class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}
} public void pop() {
int top=stk.pop();
if(minStk.peek()==top){
minStk.pop();
}
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
方法二:
class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}else{
minStk.push(minStk.peek());
}
} public void pop() {
stk.pop();
minStk.pop();
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
155 Min Stack 最小栈的更多相关文章
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- [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] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 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 ...
随机推荐
- 设计模式学习笔记——Template Method模板方法模式
可能是最简单的设计模式. 而且你我都用过而不自知. 因为,模板方法模式也者,就是面向对象中的继承.公用部分放在父类,子类继承父类,然后扩展.呵呵.
- C/C++ 操作符优先级
不能光转贴,有空要熟悉之后,要写点心得.现在发现 . 的优先级确实很高. C: Precedence Operator Description Associativity 1 ++ -- Suffix ...
- poj 1821 Fence(单调队列优化DP)
poj 1821 Fence \(solution:\) 这道题因为每一个粉刷的人都有一块"必刷的木板",所以可以预见我们的最终方案里的粉刷匠一定是按其必刷的木板的顺序排列的.这就 ...
- js 中整理(一)
一. 冒泡与冒泡阻止 var arr={5,0,-56,900,12,9000,-123}; var flag=false; //大的排序次数(arr.length-1) for(var i=0; ...
- [arm驱动]Linux内核开发之阻塞非阻塞IO----轮询操作【转】
本文转载自:http://7071976.blog.51cto.com/7061976/1392082 <[arm驱动]Linux内核开发之阻塞非阻塞IO----轮询操作>涉及内核驱动函数 ...
- POJ3376 Finding Palindromes —— 扩展KMP + Trie树
题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS Memory Limit: 262 ...
- MYSQL初级学习笔记七:MySQL中使用正则表达式!(视频序号:初级_44)
知识点九:MySQL中使用正则表达式(44) (1):REGEXP‘匹配方式’: (2):常用匹配方式: 模式字符 ^ 匹配字符开始的部分 $ 匹配字符串结尾的部分 . 代表字符串中的任意一个字符,包 ...
- ES6 数组的解构赋值
数组的解构赋值 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 以前,为变量赋值,只能直接指定值. let a = 1; let b = ...
- receive和process的过程
(一) receive最终在fuse_kern_chan.c中的fuse_kern_chan_receive函数实现,使用系统调用读取 res = read(fuse_chan_fd(ch), buf ...
- xcode添加背景音乐/音效
xcode添加音效:http://www.cnblogs.com/jiayongqiang/p/5625886.html 背景音乐: ios播放音乐时会用到一个叫做AVAudioPlayer的类,这个 ...