题目描述:

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. sd卡脱机烧写系统的方法(测试成功)

    一.sd卡烧写系统的基本思路: (1)把uboot.bin烧写到sd卡 (2)把image整个文件夹复制到sd卡 (3)开发板从sd卡启动,就开始自动烧写到nandflash中了. 二.烧写uboot ...

  2. PullToRefresh

    PullToRefreshListView的使用,实现下拉刷新,上拉加载更多.首先是布局文件: <com.handmark.pulltorefresh.library.PullToRefresh ...

  3. AngularJS包含

    1.在HTML中包含HTML文件:在HTML中,目前还不支持包含HTML文件的功能: 2.服务端包含:大多数服务端脚本都支持文件功能(SSI),使用SSI,你可以在HTML中包含HTML文件,并发送到 ...

  4. 练练脑javascript写直接插入排序和冒泡排序

    function insertionSort(array) { if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') ...

  5. Linux 压缩解压

    压缩解压 ------------------------------------------ linux 下所有的压缩格式,WinRAR 都支持 gzip .gz 格式 压缩文件: gzip 文件名 ...

  6. JS:XML

    一 IE中的XML //1.创建XMLDOM对象 //创建XMLDOM对象 var xmlDom = new ActiveXObject("MSXML2.DOMDocument.6.0&qu ...

  7. Test Driven Development

    链接:https://msdn.microsoft.com/zh-tw/library/dn743856.aspx

  8. Hadoop_UDAF示例

    UDAF: 多进一出 GenericUDAFEvaluator : 就是根据job的不同阶段执行不同的方法 Hive通过GenericUDAFEvaluator.Modle来确定job的执行阶段 PA ...

  9. 分部方法 partial

    当有如下这样类似的情况出现的时候,可以有更好的优化方式来处理,那就是分部方法 class PartOld { string name; public virtual void OnChangeName ...

  10. CI,从数据库读取数据

    1.准备数据库,(用户,密码,数据库服务的地址) 2.CI链接数据库,配置database.php(配置文件)       //application/config/database.php 3.准备 ...