设计一个支持 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.
class MinStack {

    private Stack<Integer> stack;
private Stack<Integer> stackMin;
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
} public void push(int x) {
stack.push(x);
if (!stackMin.isEmpty()) {
int min = stackMin.peek();
if (x<=min) {
stackMin.push(x);
}
} else {
this.stackMin.push(x);
}
} public void pop() {
int value = this.stack.pop();
if (!this.stackMin.isEmpty()) {
if (value == stackMin.peek()) {
stackMin.pop();
}
}
} public int top() {
int value = this.stack.peek();
return value;
} public int getMin() {
return this.stackMin.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();
*/

LeetCode155.最小栈的更多相关文章

  1. [Swift]LeetCode155. 最小栈 | Min Stack

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

  2. [LeetCode] Min Stack 最小栈

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

  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 实现最小栈

    LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...

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

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

  6. python实现时间o(1)的最小栈

    这是毕业校招二面时遇到的手写编程题,当时刚刚开始学习python,整个栈写下来也是费了不少时间.毕竟语言只是工具,只要想清楚实现,使用任何语言都能快速的写出来. 何为最小栈?栈最基础的操作是压栈(pu ...

  7. LeetCode OJ:Min Stack(最小栈问题)

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

  8. LeetCode初级算法--设计问题02:最小栈

    LeetCode初级算法--设计问题02:最小栈 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  9. LeetCode 155:最小栈 Min Stack

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

随机推荐

  1. 1 byte 8 bit 1 sh 1 bit 2. 字符与编码在程序中的实现

    https://en.wikipedia.org/wiki/Shannon_(unit) 1字节(英语:Byte)=8比特(英语:bit) The shannon (symbol Sh), also ...

  2. [development][dpdk][hugepage] 大页内存的挂载

    参考: [development][dpdk][hugepage] 为不同的结点分配不同大小的大页内存 完成了以上内容之后, 下一步需要做的是挂载, 大页内存只有被挂载了之后,才能被应用程序使用. 挂 ...

  3. es中如何定位不合法搜索

    GET /test_index/test_type/_validate/query?explain{ "query": { "math": { "te ...

  4. docker端口映射或启动容器时报错

    原始镜像如下: REPOSITORY TAG IMAGE ID CREATED SIZE xtjatswc/mycore2 v3 73ce3cd97c01 About an hour ago .74G ...

  5. dedecms清空所有文章怎么操作?sql语句如何写?

    小C新建了一个站,确切的说是复制,出于seo考虑,决定清空所有文章,那么dedecms清空所有文章怎么操作?sql语句如何写呢?特别提醒:修改之前一定要先做好备份,以防万一!下面的语句在迫不得已的情况 ...

  6. Visual Studio 10.0设置引用HalconDotNet.dll

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010435562/article/details/8858638 開始做Halcon的上位机.选用 ...

  7. 火币网API文档——REST 行情、交易API简介

    REST API 简介 火币为用户提供了一套全新的API,可以帮用户快速接入火币PRO站及HADAX站的交易系统,实现程序化交易. 访问地址 适用站点 适用功能 适用交易对 https://api.h ...

  8. NYOJ 迷宫寻宝(一)

    # include<iostream> # include<string> # include<string.h> # include<queue> # ...

  9. equals和contains的区别

    equals只能判断两个变量的值是否相等.contains常用与集合中判断某个对象是否含有这个元素equals是需要两个对象完全相同才会返回true,而contains是要循环遍历容器里的所有内容后判 ...

  10. GIT中常用的命令

    最近项目中使用到了GIT,所以记录一下GIT中常用的命令. GIT使用的客户端有Git Bash:http://code.google.com/p/msysgit/ 还有乌龟TortoiseGit:h ...