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. [译]git status

    git status git status命令能展示工作目录和stage区的状态. 使用他你能看到那些修改被staged到了, 哪些没有, 哪些文件没有被Git tracked到. git statu ...

  2. Tomcat 6 --- 使用Jasper引擎解析JSP

    熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译. 由于JVM只认识class ...

  3. PHP基础之 重载 的实现方式

    ===================PHP中的伪重载Overloading================== PHP中没有像C#或java中的重载,但可以通其它方法实现重载 重载:属性重载与方法重 ...

  4. php补充

    PHP 教程 echo 和 print 之间的差异:echo - 能够输出一个以上的字符串print - 只能输出一个字符串,并始终返回 1提示:echo 比 print 稍快,因为它不返回任何值. ...

  5. jersey

    http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html

  6. js 模块化编程

    Javascript模块化编程(一):模块的写法   作者: 阮一峰 日期: 2012年10月26日 随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞 ...

  7. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  8. hiberante学习笔记

    1.配置文件(hibernate映射文件): 让hibernate知道该怎么样去load,store持久化对象: 1.1 数据库忌讳的字段名 1) User 2) index 2.数据库表中一对多,多 ...

  9. GZIP压缩

     (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com)         zip压缩文件听说过,GZIP对我可是新鲜词儿,这个世界好复杂,压缩是无处不 ...

  10. word20161130

    1. even 偶数 [ǒu shù][词典] even; [数] even number;[例句]在本例中,slot A中有奇数个阵列,slot B中有偶数个阵列.In this example, ...