【原创】leetCodeOj --- Min Stack 解题报告
题目地址:
https://oj.leetcode.com/problems/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.
方法:
思路很简单。
维护两个栈,一个是数据栈正常进出;另一个最小栈的栈顶保存着当前数据栈中的最小值。
这么说可能看不太懂,举个例子吧。
数据栈中:9,9,3,4,4,4,5,3,3,10 其中10是栈顶。
最小栈中:9,9,3,3,3
push操作:
当前进入数据栈中的值小于等于最小栈栈顶的值的话,则也将该值压进最小栈,否则就只push数据栈。
pop操作:
如果当前数据栈栈顶的值等于最小栈栈顶的值,那么两个栈都弹值;否则只弹数据栈栈顶的值。
注意:C++中,如果使用vector,就会出现MLE错误。我换成stack就顺利过关。怀疑跟stack底层是个链表,而vector底层是动态增长数组有关。(剖析STL没看,SHIT,瞎猜一个)
全部AC代码:
class MinStack {
private:
stack<int> trueStk;
stack<int> minStk;
public:
void push(int x) {
trueStk.push(x);
if (minStk.size() == )
minStk.push(x);
else
{
int tmp = getMin();
if (x <= tmp)
minStk.push(x);
}
}
void pop() {
int tmp = top();
trueStk.pop();
if (tmp == getMin())
minStk.pop();
}
int top() {
return trueStk.top();
}
int getMin() {
return minStk.top();
}
};
【原创】leetCodeOj --- Min Stack 解题报告的更多相关文章
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【原创】leetCodeOj --- Dungeon Game 解题报告
原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- 【原创】leetCodeOj --- Sort List 解题报告
今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...
- 【原创】leetCodeOj ---Partition List 解题报告
原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...
随机推荐
- How a C++ compiler implements exception handling
Introduction One of the revolutionary features of C++ over traditional languages is its support for ...
- windows下搭建node.js及npm的工作环境
近期在研究数据可视化D3框架,决定在windows下搭建一个nodejs及npm的工作环境,在网上查了n篇文章,别管是编译源代码安装也好.还是使用node.msi格式安装包也好,总是有问题.终于,功夫 ...
- About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open source
About Unixstickers - Unixstickers - stickers on unix, programming, software, development and open so ...
- 【机器学习实验】学习Python来分类现实世界的数据
引入 一个机器能够依据照片来辨别鲜花的品种吗?在机器学习角度,这事实上是一个分类问题.即机器依据不同品种鲜花的数据进行学习.使其能够对未标记的測试图片数据进行分类. 这一小节.我们还是从scikit- ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- HDU2602 Bone Collector 【01背包】
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- LeetCode_Merge Two Sorted Lists
一.题目 Merge Two Sorted Lists My Submissions Merge two sorted linked lists and return it as a new list ...
- HDU 4815 背包
标题的含义给出N问题.和概率P,然后给予相应的分数为每个问题x(每个问题只有两种选择,纠正错误). 两个人来回答.一个人是随机选择的答案,问:还有一个人的至少一些点的能力有保证P概率不会失败. 01背 ...
- Jquery插件placeholder的用法
闲的蛋疼,演示一下Jquery插件placeholder的用法,借助该插件能够轻松实现HTML5中placeholder特效: 效果图: 实现代码: <%@ page language=&quo ...
- hdu4553(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 线段树功能:update:区间替换 query:询问满足条件的最左断点 分析:poj3667的加 ...