题目描述:

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.

解题思路:

主要是怎么保存最小值这个问题,每次在向栈中添加数据时,总是和当前的最小值比较,如果比当前的最小值小,那么则把当前的最小值压入栈中,表明当前栈中的最小值为当前最小值,然后再把新的数据压入栈中。每次弹出栈顶的元素时候,将弹出的数据与当前的最小值比较,如果相等,表明已经达到当前最小值所在的位置,下面的那个数是当前栈中的最小值,将其弹出,其值为新的最小值。

代码如下:

class MinStack {

	Stack<Integer> stack = new Stack<Integer>();
int min = Integer.MAX_VALUE; public void push(int x) {
if(x <= min){
stack.push(min);
min = x;
}
stack.push(x);
} public void pop() {
int peek = stack.pop();
if(peek == min)
min = stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return min;
}
}

  

Java [Leetcode 155]Min Stack的更多相关文章

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

  2. leetcode 155. Min Stack --------- java

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

  3. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  4. Java for LeetCode 155 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. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  7. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

  8. 155. Min Stack

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

  9. 【LeetCode】Min Stack 解题报告

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

随机推荐

  1. STL中的stack(堆栈)

    转载:http://blog.csdn.net/morewindows/article/details/6950881 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(F ...

  2. 官方 Animator 例子解析 Animator.MatchTarget

    一.官方的解释 Animator.MatchTargetSwitch to Manual ); Parameters matchPosition The position we want the bo ...

  3. Window 8.1 开启Wifi共享

    p{padding-left:20px;} Hosted network supported:Yes  支持Wifi共享 命令:netsh wlan set hostednetwork mode=al ...

  4. JAVASCRIPT中的作用域和原型链,应该算是难点了,要好好多学学,练练

    今天初六,要上班啦... JAVASCRIPT,看来是丢不了了.. http://www.dengdeng90.com/wordpress/?p=241 http://www.cnblogs.com/ ...

  5. String类的使用 Part2

    StringBuilder 类的使用 属性: namespace StringBuilderTest { class Program { static void Main(string[] args) ...

  6. JDK与JRE

    dos命令行中常见的命令: 1.dir:列出当前目录下的文件以及文件夹 2.md:创建目录(即文件夹) |-----C:\>md kkk(在C盘下创建了一个名为kkk的文件夹) 3.rd:删除目 ...

  7. Eclipse通过集成svn实现版本控制

    Eclipse通过集成svn即安装subclipse插件 前面已经讲解过了,这就不说了,作为测试人员继续总结下Eclipse通过集成svn实现的版本控制 首次从SVN代码库中导出代码文件: 1.右键工 ...

  8. H5 移动Web框架集合

    http://frozenui.github.io/  一个简洁的h5前端框架 http://weui.github.io/weui/ 腾讯出的WebUI 风格是基于微信

  9. http://blog.csdn.net/superhosts/article/details/15813247

    http://blog.csdn.net/superhosts/article/details/15813247

  10. 2014--9=17 软工二班 MyEclipse blue==4

    package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...