lintcode 中等题:Min stack 最小栈
题目
带最小值操作的栈
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。
你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。
解题
可以定义一个数组或者其他的存储最小值,第i个元素,表示栈中前i个元素的最小值。
定义两个ArrayList来存储栈,一个ArrayList存储当前栈中的元素,一个ArrayList存储最小栈,并且其第i个元素表示栈中前i个元素的最小值,这样两个栈的长度是始终一样的
入栈:最小栈需要加入的元素是 当前要入的元素和list中最后一个元素的最小值
出栈:最小栈也要出栈的,不需要进行比较,直接出栈
获取最小值:就是去栈顶元素的,直接取出list 中最后一个元素就好了
取栈顶元素:直接取
public class MinStack {
// 定义两个栈
private ArrayList<Integer> stack;
private ArrayList<Integer> minStack;
public MinStack() {
// do initialize if necessary
stack = new ArrayList<Integer>();
minStack = new ArrayList<Integer>();
}
// 入栈
public void push(int number) {
// write your code here
stack.add(number);
if( minStack.size() ==0){
minStack.add(number);
}else{
int size = minStack.size();
minStack.add(Math.min(number,minStack.get(size-1)));
}
}
// 出栈
public int pop() {
// write your code here
int size = minStack.size();
int pop = stack.get(size - 1);
minStack.remove(size - 1);
stack.remove(size - 1);
return pop;
}
// 最小值
public int min() {
// write your code here
int size = minStack.size();
return minStack.get(size - 1);
}
// 栈顶元素
public int peek(){
int size = stack.size();
return stack.get(size - 1);
}
}
Java Code
上面程序中最小栈元素保存的元素有重读,可以优化下。
九章中看到了另外一种解法,用两个栈了存储两个栈
一种程序如下,最小栈中重复数据减少了。
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
// do initialize if necessary
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int number) {
// write your code here
stack.push(number);
if( minStack.isEmpty()){
minStack.push(number);
}else if( number <= minStack.peek()){
minStack.push(number);
}
}
public int pop() {
// write your code here
int p = stack.pop();
if( p == minStack.peek())
minStack.pop();
return p;
}
public int min() {
// write your code here
return minStack.peek();
}
}
Java Code
九章中给的Python版本的就是利用list实现的
class MinStack(object):
def __init__(self):
# do some intialize if necessary
self.stack = []
self.minstack = []
def push(self, number):
# write yout code here
self.stack.append(number)
if len(self.minstack) == 0 or number <= self.minstack[-1]:
self.minstack.append(number)
def pop(self):
# pop and return the top item in stack
if self.stack[-1] == self.minstack[-1]:
self.minstack.pop()
return self.stack.pop()
def min(self):
# return the minimum number in stack
return self.minstack[-1]
Python Code
lintcode 中等题:Min stack 最小栈的更多相关文章
- [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 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- ExtJS实战 01——HelloWorld
前言 Extjs5的发布已经有些日子了,目前的最新稳定版本是Extjs5.1.0,我们可以在官方网站进行下载.不过笔者今天访问得到的是502Bad Gateway,原因可能是sencha的nigix没 ...
- CentOS 5.8 升级php版本
一:我们都知道系统的yum源安装出来的php版本不是5.1的就是5.3 那就是说 有些程序不支持那么低的版本的呢 那我们该怎么办呢 接下来 简单的说下php的版本升级 编译升级太慢了 这里我们选择 ...
- mysql中log
mysql的主从模式配置 1.改主库配置文件:D:\Program Files\MySQL\MySQL Server 5.5(my.ini/my.cnf)在下面加入 [mysqld] log=c:/a ...
- Highcharts条形与柱形同时显示
var chart; $(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'chart_combo' //关联页面元素di ...
- 转发 python中file和open有什么区别
python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...
- PHP正则表达式的逆向引用与子模式 php preg_replace应用
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) 功能 在 subject 中搜索 ...
- 实现iOS长时间后台的两种方法:Audiosession和VOIP(转)
分类: Iphone2013-01-24 14:03 986人阅读 评论(0) 收藏 举报 我们知道iOS开启后台任务后可以获得最多600秒的执行时间,而一些需要在后台下载或者与服务器保持连接的App ...
- 深入理解ThreadLocal(二)
3 InheritableThreadLocal的使用 通过上面的分析知道通过ThreadLocal保存的值是线程隔离的.其实在Thread对象中,还有一个ThreadLocal.ThreadLoca ...
- Using self-defined Parcelable objects during an Android AIDL RPC / IPC call
Using self-defined Parcelable objects during an Android AIDL RPC / IPC call In my previous post “Usi ...
- text-align:justify实现文本两端对齐布局,兼容IE
要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清楚的概念.大多 IE下的显示错误,就是源于 haslayout. 什么是 haslayout ...