题目描述:

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.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2. 解法一:
用一个栈s和一个指示当前栈中最小值min_elem的int,在push操作或者pop操作中更新min_elem。
class MinStack {
public:
/** initialize your data structure here. */
int min_elem;
stack<int> s; MinStack() {
min_elem = INT_MIN;
} void push(int x) {
if (min_elem == INT_MIN) {
min_elem = x;
}
else if (x < min_elem)
{
min_elem = x;
}
s.push(x); } void pop() {
if (s.empty()) {
return;
}
else
{
s.pop();
min_elem = INT_MIN;
stack<int> temp_s;
while (!s.empty()) {
temp_s.push(s.top());
if (min_elem == INT_MIN) {
min_elem = s.top();
}
else if (s.top() < min_elem) {
min_elem = s.top();
}
s.pop();
} while (!temp_s.empty()) {
s.push(temp_s.top());
temp_s.pop();
}
} } int top() { return s.top(); } int getMin() { return min_elem; }
};

这个解法不是很好的解法,虽然能够AC,但是效率在leetcode网站上的排名很低……

解法二:

使用两个栈,s和min。min记录当前栈中的一个递减序列,因为最小值的出栈操作仅和这个递减序列有关。

class MinStack {
public:
/** initialize your data structure here. */
stack<int> s;
stack<int> min; MinStack() { } void push(int x) {
if (s.empty() || x <= getMin()) {
min.push(x);
}
s.push(x); } void pop() {
if (s.top() == min.top()) {
min.pop();
}
s.pop();
} int top() {
return s.top(); } int getMin() {
return min.top(); }
};

这个版本是leetcode官方的版本,但是效率也不是最好的,但是真的非常简洁,再次印证了简洁即高效的代码准则。

 

leetcode 155的更多相关文章

  1. LeetCode 155:最小栈 Min Stack

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

  2. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  4. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  5. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  6. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  7. LeetCode(155)题解--Min Stack

    https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrie ...

  8. Java实现 LeetCode 155 最小栈

    155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...

  9. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

随机推荐

  1. winform用户控件、动态创建添加控件、timer控件、控件联动

    用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...

  2. webbench之编译安装(一)

    1.编译安装:   1 2 3 4 [root@hexuweb102 ~]$wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar ...

  3. 版本管理之Git(二):Win7上Git安装及简单配置过程

    一.安装包 msysgit(Windows版本的Git) 下载地址:http://code.google.com/p/msysgit/downloads/list?q=full+installer+o ...

  4. Oracle 取随机数

    1.从表中随机取记录 select * from (select * from staff order by dbms_random.random)      where rownum < 4 ...

  5. BZOJ2454 : TopCoder SRM 463 RabbitPuzzle

    每种状态最多只有三种后继状态:中间往左跳,中间往右跳,两边往中间跳. 如果把它们分别看成左儿子.右儿子.父亲的话,那么会得到一些二叉树. 取出起始状态和终止状态往上跳$k$步的所有状态,其他状态我们只 ...

  6. 【转】java-String中的 intern()

    转自:http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html 1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值 ...

  7. 【水】基于ege的简单3D模拟

    我们模拟从被观察物体射出光线,在眼球焦点交汇,然后打到视网膜上成像 ——足够了吧,剩下的难度应该是普及- 只是有一些常数可以自己调一下,看着顺眼就好 #include <graphics.h&g ...

  8. 【BZOJ1087】 [SCOI2005]互不侵犯King 状压DP

    经典状压DP. f[i][j][k]=sum(f[i-1][j-cnt[k]][k]); cnt[i]放置情况为i时的国王数量 前I行放置情况为k时国王数量为J #include <iostre ...

  9. css记录

    padding padding-top是在绿色边框内,从顶部向下移20像素位置,默认padding-top 为0时,红色边框为20像素高,通过padding-top属性,为顶部增加了20像素,这时顶部 ...

  10. How does Unity.Resolve know which constructor to use?

    Question: Given a class with several constructors - how can I tell Resolve which constructor to use? ...