【Leetcode】【Easy】Min Stack
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- 【算法笔记】B1041 考试座位号
1041 考试座位号 (15 分) 每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生 ...
- 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】
组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n\) 个穴位,穴位的编号为 \(1\sim n\).有若干条有向的脉络连接着这些穴位. ...
- P3345 [ZJOI2015]幻想乡战略游戏
传送门 考虑先随便找一个点作为根,然后再慢慢移动根,这样一步步走到最优的点 设 $sum[x]$ 表示节点 $x$ 的子树的军队数,$len(x,y)$ 表示 $x,y$ 之间边的长度 那么对于根节点 ...
- Echarts图表横坐标显示不全
xAxis: { "axisLabel":{ //加上这个强制显示 interval: 0 }, type: 'category', data: self[theDataKey]. ...
- VBS修改本机的账号密码
On Error Resume Next strComputer = "." Set WshShell = WScript.CreateObject("WScript.S ...
- [转] Spring Boot特性
[From] http://blog.javachen.com/2015/03/13/some-spring-boot-features.html 1. SpringApplication Sprin ...
- storm(2)-机制
1.storm vs esper 2.storm vs spark streaming storm处理的是每次传入的一个事件:spark streaming 处理的是某个时间段窗口内的事件流. 因此 ...
- Win10磁盘利用率高达100%设置修改方法
Win10磁盘利用率高达100%设置修改方法 Windows Defender关闭 Win10的Windows Defender已经变身成为安全中心,它的运行机制改成了即便是电脑中存在多个杀毒.防护软 ...
- echarts Y轴数据类型不同怎么让折线图显示差距不大
如果希望在同一grid中展示不同数据类型的折线(1000或10%),那么展现出来的折线肯定显示差距很大,那么怎么让这两条折线显示效果差不多,在之前的项目中碰到了这个问题 每条折线对应的是不同的数据组, ...
- oracle 备份恢复篇(二)---rman 增备恢复--不完全恢复
一,环境准备 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=prod expor ...