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.

实现一个栈的基本功能,并且可以用o(1)的时间取出栈的最小元素。

使用两个栈的解题方法:

建立两个栈容器,一个常规使用elementStack,一个保存栈内最小元素minStack。

注意:

常规栈中最小的元素可能不唯一。当某个最小元素出栈时,minStack也需做出出栈动作。因此minStack栈中记录最小元素的数量应该和elementStack中最小元素数量相同。

也就是,每次进行常规栈压栈时,都进行和min栈记录的最小元素比较,将小于等于min栈最小元素的常规元素压栈。

代码:

 class MinStack {
public:
void push(int x) {
element.push(x);
if (min.empty() || x <= min.top())
min.push(x);
} void pop() {
if (!element.empty()) {
if (element.top() == min.top())
min.pop();
element.pop();
}
} int top() {
if (!element.empty())
return element.top();
} int getMin() {
if (!min.empty())
return min.top();
} private:
stack<int> element;
stack<int> min;
};

也可以只使用一个栈,即常规栈。只是压栈的每个元素都包含当前栈内最小元素值。

即,假设当前栈内最小值为m,当对元素A压栈时,若A<=m,则压入 (A, A),若A>m,则压入 (A, m)。

(代码略)

附录:

各种数据结构最简洁表示方法,参考《数据结构与算法分析—C语言版》

【Leetcode】【Easy】Min Stack的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. LeetCode 155:最小栈 Min Stack

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

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  8. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. UVALive - 7061 区间DP初步

    题意:杀一只狼i会收到a[i]+b[i当前左边]+b[i当前右边]的攻击,求杀死所有狼的最小代价 #include<iostream> #include<algorithm> ...

  2. UVA - 10298 后缀数组(仅观赏)

    题意:求最小循环节 \(KMP\)可以20ms通过,而\(da\)实现的后缀数组并无法在3000ms内通过 听说要用\(dc3\)才勉强卡过,这里仅列出\(da\)实现 #include<ios ...

  3. Spring对外部属性文件指定的某个属性进行加密、解密

    [From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...

  4. 关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds

    [From] http://blog.csdn.net/vtopqx/article/details/8522333 闲来无事测试了下Ehcache与MemCache比较,在此发现了Ehcache中一 ...

  5. nodejs下载器,通过chrome代理下载http资源

    var config={ //不想访问的东西,节约流量 "404":[ "http://qidian.qpic.cn/qdbimg" ], //奇数为需要下载的 ...

  6. python爬虫之认识爬虫和爬虫原理

    python爬虫之基础学习(一) 网络爬虫 网络爬虫也叫网络蜘蛛.网络机器人.如今属于数据的时代,信息采集变得尤为重要,可以想象单单依靠人力去采集,是一件无比艰辛和困难的事情.网络爬虫的产生就是代替人 ...

  7. oracle 单实例DG(切换篇三)

    一,开篇 此篇操作承接上文,必须完成DG实例搭建完成方可有执行以下内容的实例 二,切换物理备库 one. oracle01库命令 select switchover_status from v$dat ...

  8. 【3dsMax安装失败,如何卸载、安装3dMax 2010?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  9. 【XShell】xshell 中“快速命令集”的使用

    突然看到朋友的xshell比我多一个按钮,且一点,哈哈哈 ,实现了很炫酷的功能,耐不住好奇,问了一句,原来是快速命令集! 1.选择快速命令集(两种方法a&b) a:文件 > 属性 > ...

  10. 03.枚举和string以及int类型之间的转换

    练习1:   将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...