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(能返回最小数的栈)的更多相关文章

  1. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  2. 带最小值操作的栈 · Min Stack

    [抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...

  3. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

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

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

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

  6. 栈 堆 stack heap 堆内存 栈内存 内存分配中的堆和栈 掌握堆内存的权柄就是返回的指针 栈是面向线程的而堆是面向进程的。 new/delete and malloc/ free 指针与内存模型

    小结: 1.栈内存 为什么快? Due to this nature, the process of storing and retrieving data from the stack is ver ...

  7. [LintCode] Min Stack 最小栈

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

  8. [LeetCode] Min Stack 栈

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

  9. 155. Min Stack

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

随机推荐

  1. 2015.3.31不使用debug/X86文件夹方式解决64/32位问题

    传统方法:在解决方案-配置管理器-新建X86平台可以解决32位兼容问题,但是Debug目录下会多出X86文件夹.不方便 另一种方法:在项目名称-属性-生成-目标平台-x86也能解决问题,而且不出现X8 ...

  2. NT AUTHORITY\IUSR登录失败解决方法

    NT AUTHORITY\IUSR登录失败解决方法 1 今天打开网站时,突然报这个错误,平时都好好的 Cannot open database "aslkm" requested ...

  3. DAY15-HTTP协议简述

    HTTP协议 一.HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网 ...

  4. Mediaplayer

    Mediaplayer报错 prepareAsync called in state 1     是因为在setDataSource之前调用了prepare.因为setDataSource放到了线程里 ...

  5. Maven学习笔记1-牛逼的POM文件

    IDE:integrated development enterprise 集成开发环境: 一.pom文件 POM是项目对象模型(Project Object Model)的简称:maven世界中必须 ...

  6. 51NOD 1616 最小集合

    传送门 分析 不难发现集合中的数一定是集合内其它一堆数的$gcd$ 于是我们枚举$i$,统计原来集合中有几个数是$i$的倍数,设这个值为$f(i)$ 之后对于每个$i$如果不存在$f(x*i) = f ...

  7. centos安装中文字体

    1.查看字体列表 2.将需要上传的字体上传至linux服务器/usr/share/fonts/chinese目录下 3.修改chinese目录的权限 chmod -R /usr/share/fonts ...

  8. java全栈day01-03注释、关键字与标识符

    通常我们需要在源代码中添加文字用来对进行代码解释说明,但这些文字并不是Java代码的语法,会导致编译出错.这时我们可以使用注释来完成这一事项! 在编译时,编译器会忽略注释的存在,就好像注释内容不存在一 ...

  9. 启动应用程序的Activty多种方式

    启动应用程序的Activity总共有三种方式,如下: 1>通过包名得到将启动应用的入口Activity,然后给intent附上相应的属性即可. 示例代码 public static void s ...

  10. DataGridView增加全选列

    最近的一个winform的项目中,碰到datagridview控件的第一列添加全选的功能,通常这个功能,有两种实现方式:1. 为控件添加DataGridViewCheckBoxColumn来实现,但是 ...