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 {
public:
void push(int x) {
if(mins.empty() || x <= mins.top())
mins.push(x);
s.push(x);
} void pop() {
if(!s.empty() && !mins.empty() && s.top() == mins.top())
mins.pop();
s.pop();
} int top() {
return s.top();
} int getMin() {
return mins.top();
}
private:
stack<int> s;
stack<int> mins;
};

【leetcode】Min Stack(easy)的更多相关文章

  1. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  2. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  5. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  6. 【leetcode】Min Stack -- python版

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

  7. 【LeetCode】Min Stack 解题报告

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

  8. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  9. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

随机推荐

  1. Apache中,同一IP使用多域名对应多个网站的方法

    首先dns中确定有相应的A记录, abc  IN A   211.154.2.5 mail IN A   211.154.2.5 这个讲的是在windows下面配置apache虚拟主机: 一.配置虚拟 ...

  2. AD域服务器|两台DC无法进行复制同步

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...

  3. 控制器View的生命周期及相关函数是什么?你在开发中是如何用的?

    * 1.首先判断控制器是否有视图,如果没有就调用loadView方法创建:通过storyboard或者代码: * 2.随后调用viewDidLoad,可以进行下一步的初始化操作:只会被调用一次: * ...

  4. 示波器trigger的使用方法

    背景: 下位机有俩个IO口设置为外部中断——边沿触发.低电平有效.因此我需要抓取下降沿波形,但低电平时间很短,手动暂停抓取不仅不科学还费力,那么该如何准确的抓取到呢?最好的办法是使用示波器的trige ...

  5. Objective-C上地球坐标系到火星坐标系转换算法

    Objective-C上地球坐标系到火星坐标系转换算法 http://blog.csdn.net/zhaoxy_thu/article/details/17033347

  6. window.location.href 和 window.location.replace 的区别

    window.location.href  和  window.location.replace 的区别 1.window.location.href=“url”:改变url地址: 2.window. ...

  7. iOS开发——多线程篇——RunLoop

    一.简介 1.什么是RunLoop从字面意思看运行循环跑圈 基本作用保持程序的持续运行处理App中的各种事件(比如触摸事件.定时器事件.Selector事件)节省CPU资源,提高程序性能:该做事时做事 ...

  8. 语义化HTML

    一.怎样语义化html结构 语义化是指用合理HTML标签以及其特有的属性去格式化文档内容. 正确语义化----结构(html)才是重点,样式(css)是用来修饰结构的.所以要先确定html,确定标签, ...

  9. Unity手游之路<十三>手游代码更新策略探讨

    http://blog.csdn.net/janeky/article/details/25923151 这几个月公司项目非常忙,加上家里事情也多,所以blog更新一直搁置了.最近在项目开发上线过程中 ...

  10. sizeof和小部分c++语法

    ios 中 sizeof(类型) 获取类型所占字节 32位操作系统中, 每个字节占8个位 64位操作系统中, 每个字节占16个位 INT_MAX  2147483647 int size1 = siz ...