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. Intelligent Poetry

    Readme: Creat poems. import re import random from collections import Counter def Creat_Poem(number): ...

  2. 1076 Wifi密码 (15 分)

    下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一 ...

  3. wamp下localhost目录Your Projects下项目无法打开解决方案

    最近在学PHP,然后可能遇到各种小白问题,记录下来当做自己成长的见证吧: wamp下localhost目录Your Projects下项目无法打开,但是在url中输入项目可以访问到. 解决方案: 注意 ...

  4. [转] domeOS 环境搭建 自动化构建部署

    [From]http://dockone.io:82/article/4150 系统:CentOS Linux 7A机子(domeos服务器):1. gitlab安装(私有仓库):yum -y ins ...

  5. Hibernate 4 Second Level Caching With EHCache

    Hibernate 4 Second Level Caching With EHCache [From] http://www.codesenior.com/en/tutorial/Hibernate ...

  6. 【CTF】某xss练手小游戏

    http://test.xss.tv 1.http://47.94.13.75/test/level1.php?name=test 直接插入即可,如: http://47.94.13.75/test/ ...

  7. PIE SDK矢量自定义渲染

    1. 功能简介 PIE SDK中关于矢量渲染提供了多种方案,包括简单渲染.分级渲染.唯一值渲染,这几种渲染方式具有一定的通用性,可以满足用户绝大多数的需求. 当面对复杂的业务,当前渲染方案无法满足用户 ...

  8. layui 数据返回但是table表格未渲染出来的问题

    最近蜂鸟速驾项目用layui框架,table进行表格渲染 控制台打印发现有数据,但是table.render渲染不出来,后来发现是数据已经返回但是页面元素未加载完成的缘故,用setTimeout定时器 ...

  9. redis安装配置远程连接

    一.安装redis linux上直接yum安装 yum install redis windows版本下载地址 https://github.com/ServiceStack/redis-window ...

  10. SpringMvc上传文件遇到重复读取InputStream的问题

    文件上传配置: <bean id="multipartResolver" class="org.springframework.web.multipart.comm ...