设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈。
    push(x) -- 将元素x推入栈中。
    pop() -- 删除栈顶的元素。
    top() -- 获取栈顶元素。
    getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
详见:https://leetcode.com/problems/min-stack/description/

Java实现:

方法一:

class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}
} public void pop() {
int top=stk.pop();
if(minStk.peek()==top){
minStk.pop();
}
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

方法二:

class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}else{
minStk.push(minStk.peek());
}
} public void pop() {
stk.pop();
minStk.pop();
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

155 Min Stack 最小栈的更多相关文章

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

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

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

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

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

  4. [LeetCode] Min Stack 最小栈

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

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

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

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

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

  7. [LintCode] Min Stack 最小栈

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

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

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

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

随机推荐

  1. javascript模块化编程:CommonJS和AMD规范

    AMD规范,异步模块定义.与CommonJS规范齐名并列. 作用都是利于JavaScript的模块化编程. 模块化编程的好处就是: 1.可重用 2.独立 3.能解决加载的依赖性问题 4.能解决重复加载 ...

  2. Java中的常用异常处理方法

    觉得自己是一个Java专家吗?是否肯定自己已经全面掌握了Java的异常处理机制?在下面这段代码中,你能够迅速找出异常处理的六个问题吗? 1 OutputStreamWriter out = ... 2 ...

  3. Lily HBase Indexer同步HBase二级索引到Solr丢失数据的问题分析

    一.问题描述二.分析步骤2.1 查看日志2.2 修改Solr的硬提交2.3 寻求StackOverFlow帮助2.4 修改了read-row="never"后,丢失部分字段2.5 ...

  4. (25) java web的struts2框架的使用-基于表单的文件上传

    一,首先创建一个表单页面 <body> <form action="uploads" method="post" enctype=" ...

  5. SpringBoot使用logback日志记录

    在resources里的配置文件: logback-spring.xml <?xml version="1.0" encoding="UTF-8" ?&g ...

  6. html5--6-23 CSS3中的文字与字体

    html5--6-23 CSS3中的文字与字体 text-overflow 设置是否使用一个省略标记(...)标示对象内文本的溢出 clip: 默认值当对象内文本溢出时不显示省略标记(...),而是将 ...

  7. laravel的核心概念:服务提供者provider解析

    我们知道laravel和核心就是一个IoC容器, 被称之为服务容器. 那么作为IoC容器, 就必须要有绑定对象生成器的工作.  在laravel中是服务提供者来项服务容器中绑定对象生成器的. 百牛信息 ...

  8. apple-touch-startup-image 制作iphone web应用程序的启动画面

    为ipad制作web应用程序的启动画面时发现个问题,只能显示竖屏图,横屏图出不来,如下: 首先页面头部里要加入(这个是APP启动画面图片,如果不设置,启动画面就是白屏,图片像素就是手机全屏的像素) & ...

  9. Hibernate 4.3.7 可编程方式+注解

    1.复制jar文件到lib antlr-2.7.7.jardbmysql.jardboracle.jardbsqljdbc2005.jardom4j-1.6.1.jarhibernate-common ...

  10. android调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so (转载)

    转自:http://blog.csdn.net/jiuyueguang/article/details/9449737 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言 1:本文主要作为 ...