题目

带最小值操作的栈

实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。

你实现的栈将支持pushpop 和 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 最小栈的更多相关文章

  1. [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 ...

  2. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  3. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

  4. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. 第30题:LeetCode155. Min Stack最小栈

    设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...

  7. 155 Min Stack 最小栈

    设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈.    push(x) -- 将元素x推入栈中.    pop() -- 删除栈顶的元素.    top() -- 获取 ...

  8. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

随机推荐

  1. jQuery对Select操作大集合

    介绍了jQuery对Select的操作进行了详细的汇总. 1.jQuery添加/删除Select的Option项: 2.$("#select_id").append("& ...

  2. centos 普通用户添加sudo权限

    本文介绍下,在centos中为普通用户添加sudo权限的方法,供大家学习参考. 在centos中为普通用户增加sudo权限的简单方法,大家参考下. 1,修改/etc/sudoers文件,必须为visu ...

  3. How to disable Passwords must meet complexity requirements[windows 7]

    The Password complexity is a Local Policy setting named "Passwords must meet complexity require ...

  4. Oracle中的通配符

    这是scott用户下的EMP表 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 1980/12/17 800   ...

  5. 【iis错误码】IIS 服务 这些年遇到的错误码

      II 发生错误,客户端似乎有问题.例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息. 400 - 错误的请求. 401 - 访问被拒绝.   -- 暴力添加everyone用户,  i ...

  6. return的用法

    1.一般的就是用在有返回值的方法中,用来返回方法指定类型的值,同时结束方法执行: 2.可以用在返回值为void的方法中,用来终止方法运行:

  7. thinkphp对数据库操作有哪些内置函数

    getModelName() 获取当前Model的名称 getTableName() 获取当前Model的数据表名称 switchModel(type,vars=array()) 动态切换模型 tab ...

  8. 特征值分解,奇异值分解(SVD)

    特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. 1. 特征值: 如果说一个向 ...

  9. Windows下将txt导入MySQL及远程连接设置

    1.修改字符编码,全部修改为gbk.这样修改,重启后又会恢复默认值. show variables like '%char%'; set character_set_database=gbk; 其中, ...

  10. 学习Linux第一天

    1.简介: 记住这个名字:Linus Torvals 系统组成:Linux内核,Shell, 文件系统,实时程序 Tips:在系统启动过程中,使用Alt+F2组合键,可以查看Ubuntu启动的详细过程 ...