翻译

设计支持push、pop、top和在常量时间内检索最小元素的栈。

push(x) —— 推送元素X进栈
pop() —— 移除栈顶元素
top() —— 得到栈顶元素
getMin() —— 检索栈的最小元素

原文

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.

分析

之前写过两道题,各自是用Stack来实现Queue的功能以及用Queue来实现Stack的功能,这里假设也使用两个Stack的话就很easy了。

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

class MinStack {
public:
stack<int> allStack;
stack<int> minSta; void push(int x) {
if (allStack.empty()) {
allStack.push(x);
minSta.push(x);
}
else {
allStack.push(x);
if (x <= minSta.top()) minSta.push(x);
}
} void pop() {
if (allStack.top() == minSta.top()) {
minSta.pop();
}
allStack.pop();
} int top() {
return allStack.top();
} int getMin() {
return minSta.top();
}
};

除了上面的stack,我还用vector实现了:

class MinStack {
public:
vector<int> allVec;
vector<int> minVec; void push(int x) {
if (allVec.empty()) {
allVec.push_back(x);
minVec.push_back(x);
}
else {
if (x <= minVec[minVec.size() - 1]) {
minVec.push_back(x);
}
allVec.push_back(x);
}
} void pop() {
if (allVec[allVec.size() - 1] == minVec[minVec.size() - 1])
minVec.erase(minVec.end() - 1);
allVec.erase(allVec.end() - 1);
} int top() {
return allVec[allVec.size() - 1];
} int getMin() {
return minVec[minVec.size() - 1];
}
};

然而用vector效率反而更低了……

LeetCode 155 Min Stack(最小栈)的更多相关文章

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

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

  2. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

  3. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  4. 155 Min Stack 最小栈

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

  5. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  6. 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 ...

  7. [LeetCode] Min Stack 最小栈

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

  8. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  9. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

随机推荐

  1. gitignore : VisualStudio.gitignore

    https://github.com/github/gitignore/blob/master/VisualStudio.gitignore ## Ignore Visual Studio tempo ...

  2. android 高清壁纸设置慢

    由于项目的需要最近在解决一个 bug  在1080p 的手机上面设置壁纸会很慢慢,慢的原因是和壁纸 的大小 有关,壁纸越大,时间直越长,一般1080 p 的壁纸大概有10M左右, 所以通过文件流 来保 ...

  3. Unity3d 换装 之 模型动画分离

    在手游中换装成了越来越不可缺的一个功能,毫无疑问各式各样的时装为游戏增添了不同的色彩. 对于2D手游,或许是更换对应的序列帧,也或许是如同3D手游一般,更换模型动画. 对于游戏中的人物,一般分为头.上 ...

  4. 简单的python2.7基于bs4和requests的爬虫

    python的编码问题比较恶心. decode解码encode编码 在文件头设置 # -*- coding: utf-8 -*-让python使用utf8. # -*- coding: utf- -* ...

  5. [翻译] YLGIFImage 高效读取GIF图片

    YLGIFImage 高效读取GIF图片 https://github.com/liyong03/YLGIFImage Asynchronized GIF image class and Image ...

  6. Andorid之Annotation框架初使用(三)

    线程使用: @Background这个是使用了cached thread pool executor , 阻止开启过多的线程 可以为@Background指定一个id,用于随时终止线程的操作(Back ...

  7. Python学习(四)数据结构 —— str

    Python 字符串 str 本章大致介绍了 Python 的字符串类型 str,包括字符串的赋值及转义.字符串运算符.字符串格式化输出 以及 一些字符串的内建函数等. 字符串赋值及转义 我们可以使用 ...

  8. 科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

    UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三 ...

  9. 小课堂week19 编程范式巡礼最终季 超级范式

    编程范式巡礼(最终季)--超级范式 本周是编程范式系列的最后一次分享,让我们拉长视角,看向远方,进入"元编程"的领域,在<冒号课堂>中起了个很酷的名字:"超级 ...

  10. Linux 内存泄露小结

    本文仅限记录自己的一次 内存泄露追踪小记. 可能并不十分适用与大家的情况.而且方法也并不是很smart.仅做记录,能提供个思路更好.        一. 要问调试程序遇到什么问题最头疼, 内存泄露肯定 ...