翻译

设计支持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. 220V和380V电器设备电流计算方法

    220V和380V电器设备电流计算方法 1)单相电机电流=功率/(电压*功率因数*效率): 2)三相电机电流=功率/(1.732*电压*功率因数*效率): 3)空载电流为额定电流的30-50%左右: ...

  2. 三款工作流引擎比较:WWF、netBPM 和 ccflow

    下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这三款工作流程引擎分别是 Windows Workflow Found ...

  3. SharePoint_Config_Log file size

    Been doing a routine check of my servers, and noticed that our SharePoint server was lacking some fr ...

  4. 单击行,自己主动选中当前行中的单选框button

    需求:单击行,自己主动选中当前行中的单选框button. aspx页面: <asp:Repeater ID="rptRecordList" runat="serve ...

  5. (转)SQL查询案例:多行转换为一行

    原文:http://www.cnblogs.com/sammon/archive/2012/05/10/2494362.html 测试表与测试数据 CREATE TABLE TestTitle ( n ...

  6. [Android Pro] Android--Sensor传感器

    Android提供了对设备传感器的支持,只要Android设备的硬件提供了这些传感器,Android应用可以通过传感器 来获取设备的外界条件,包括手机的运行状态.当前摆放的方向等.Android系统还 ...

  7. JavaScript的学习要点

    概要 了解Javascript历史以及Javascript三个不同组成部分: ECMAScript DOM(文档对象模型) BOM(浏览器对象模型) ECMAScript 目标 掌握Javascrip ...

  8. java中的char占几个字节

    1:“字节”是byte,“位”是bit : 2: 1 byte = 8 bit : char 在Java中是2个字节.java采用unicode,2个字节(16位)来表示一个字符. 例子代码如下: p ...

  9. SQL用例集锦

    SQL 语句分类 DDL - 数据定义语句 (CREATE,ALTER,DROP,DECLARE) DML - 数据操纵语句 (SELECT,DELETE,UPDATE,INSERT) DCL - 数 ...

  10. [置顶] JDK工具(零)--简要介绍JDK1.6自带的42个工具

    Java的开发人员肯定都知道JDK的bin目录中有“java.exe”和“javac.exe”这两个命令行工具, 但并非所有的Java程序员都了解过JDK的bin目录之中其它命令行程序的作用. JDK ...