题目描述:

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. SQL Server 之 DBCC

    --检查索引碎片情况 dbcc showconfig(tablename) 具体例子: --上图为碎片整理之前 ALTER INDEX ALL on Citation REBUILD --下图为碎片整 ...

  2. 【WCF--初入江湖】08 并发与实例模式

    08 并发与实例模式 1. 实例上下文模式   一个服务代理:servicePoxy ChannelFactory<IService1> factoryservicel = new Cha ...

  3. grub,mbr的那些事

    今天遇到一个问题是:双系统为win10和Ubuntu.启动模式为mbr,当前可以启动win10,但不能启动Ubuntu.先利用easybcd重新添加了一个,想着依旧用win10的启动项,(此处可以参考 ...

  4. C# 委托的”四步走“

    看了一本<深入了解C#>感觉很不错,对于委托的讲解,给大家摘录了下来! 1.什么是委托 我的拙见:委托就是将方法作为参数,进行传递的 书中的记载:将某种行为“包含”在一个对象中,这个对象可 ...

  5. 用django-tinymce搞个富文本编辑器

    玩过一圈之后,这些应用慢慢变得简单: 步骤如下: 一,安装: pip install django-tinymce 二,配置APP: INSTALLED_APPS = ( ... 'tinymce', ...

  6. 00 - Oracle体系结构课程内容

    1.导论 自己把本书内容分为几个部分, (1)体系结构,(2)并发控制和事务,(3)物理存储, (4)其他. 嗯,拿到一个新的数据库,我们也要从这几个方面去认识它.   2.体系结构 3.Oracle ...

  7. JAVA多线程的问题以及处理【转】

    http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理          多线程编程为程序开发带来 ...

  8. QAQ数论模板笔记√

    #include <cstdio> using namespace std; long long gcd(long long a, long long b) { // (a, b) ret ...

  9. Spring的父子容器问题

    在ssm框架搭建的时候 配置了一个Spring容器,又配置了一个前端控制器 <!-- 初始化spring容器 --> <context-param> <param-nam ...

  10. svn:...target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)

    在上传项目到svn时,pom.xml报错