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 reflog

    用法 git reflog 显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git ...

  2. SAS学习笔记<一>

    三个周末的SAS课程宣告结束, 总结下来 第一周的统计原理简介 第二周/第三周讲解SAS的基本操作. 总体下来,对自己的知识结构有了一个新的梳理,对比大学时期,某个老师一上来就教我们SAS编程,而未考 ...

  3. Javascript面向对象编程一:基础篇

    该随笔分为以下四部分: Javascript面向对象编程一:基础篇 Javascript面向对象编程二:封装 Javascript面向对象编程三:继承 Javascript面向对象编程四:控件 先弄个 ...

  4. 一张图告诉你,只会jQuery还不够!

    会了jquery语法,会了jquery函数,你就真的会了jquery吗,来看这张图!是超实用的jquery代码段一书的导览!熊孩子们,赶紧学习去吧! 对于码农来说,代码就是生产力,你每天能码多少行并不 ...

  5. CSS只是要点-收集

    1. CSS 浮动定位详解 请点击:css浮动定位详解

  6. Swift2.1 语法指南——可空链式调用

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. tcp三次握手与四次挥手

  8. Javascript高级程序设计——面向对象小结

    ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...

  9. java文档

    http://www.boyunjian.com/javadoc/com.dyuproject.protostuff/protostuff-me/1.0.5/_/com/dyuproject/prot ...

  10. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...