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.

Solution:  若只维护一个栈stk,在每次getMin的时候检索stk需要两倍的临时空间,倒两次,故用两个栈,一个stk,一个min

 class MinStack {
public:
void push(int x) {
stk.push(x);
if(min.empty()||x<=min.top())min.push(x); //if的两个判断条件顺序不能替换,否则stk添加第一个元素后getMin出错
}
void pop() {
if(stk.top()==min.top()){
stk.pop();
min.pop();
}else
stk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return min.top();
}
private:
stack<int> stk;
stack<int> min;
};

【leetcode】155 - Min Stack的更多相关文章

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

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

  2. 【一天一道LeetCode】#155. Min Stack

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Design ...

  3. 【leetcode❤python】 155. Min Stack

    #-*- coding: UTF-8 -*- class MinStack(object):    def __init__(self):        """      ...

  4. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  5. LeetCode题解 #155 Min Stack

    写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...

  6. 【Leetcode】746. Min Cost Climbing Stairs

    题目地址: https://leetcode.com/problems/min-cost-climbing-stairs/description/ 解题思路: 官方给出的做法是倒着来,其实正着来也可以 ...

  7. 【LeetCode】155. 最小栈

    155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...

  8. 【LeetCode】946. Validate Stack Sequences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟过程 日期 题目地址:https://leetc ...

  9. 【LeetCode】225. Implement Stack using Queues 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. eclipse安装Gradle

    第一步:下载Gradle>http://gradle.org/gradle-download 第二步:解压gradle-2.5, 配置环境变量:GRADLE_HOME path添加;%GRADL ...

  2. OpenMp并行提升时间为什么不是线性的?

    最近在研究OpenMp,写了一段代码,如下: #include<time.h> #include<stdio.h> #include<stdlib.h> #incl ...

  3. mongoDB使用复制还原数据库节省空间

    用db.copyDatabase可以备份复制数据的方法. 1.db.copyDatabase("from","to","127.0.0.1:16161 ...

  4. leetcode:Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. linux学习之centos(一):在VMware虚拟机中安装centos6.5

    ---安装环境如下--- 虚拟机版本:VMware Workstation_10.0.3(版本信息介绍:VMware-workstation-full-10.0.3-1895310,下载地址:http ...

  6. Java深入学习之--初始化

    目录 1.方法重载 2.默认构造器 3.this关键字 4.static关键字 5.初始化 1.方法重载 java中方法重载的意思是在同一个类中可以存在方法名相同的方法,而方法的参数类型不同,即使两个 ...

  7. the specified child alread has a parent

    用 TestFragment   extends  Fragment     @Override     public  View onCreateView(LayoutInflater inflat ...

  8. 结构体 lock_t;

    typedef struct lock_struct lock_t; //利用typedef定义一个变量的类型 /** Lock struct */ struct lock_struct { trx_ ...

  9. core--作业

    线程被封装在进程中,进程能不能被封装? 当有多个进程协调一起来完成一项任务的时候,就使用"作业"来完成 作业将进程组合在一起,并创建一个"盒子"来限制进程能够做 ...

  10. 在eclipse如何删除无效的maven build

    在Eclipse的maven项目中,点击一次“maven build...”明明没有配置,它也就会产生一个maven build,那么如何删除这些无效的配置呢?