题目描述:

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. VMware如何实现和主机共享网络上网

    VMware虚拟机的三种联网方法及原理 一.Brigde--桥接  :默认使用VMnet0 1.原理: Bridge  桥"就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在& ...

  2. 试听笔记:javascript入门精通

    一.数据类型 1.原始类型:number.string.boolean.null.undefined 2.对象类型:Object (Function.Array.Date...) P.类型隐式转换:' ...

  3. LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...

  4. java基础-包

    浏览以下内容前,请点击并阅读 声明 为了使类型更容易查找和使用,避免命名冲突,以及可视范围的控制,程序员一般将相关的一些类型组合到一个包中.组合的类型包括类,接口,枚举和注释,枚举是一种特殊的类,而注 ...

  5. Bugtags 测试平台(支持ios、android)

    官网:https://bugtags.com/ 注意:小米手机 授权 打开漂浮窗 App 集成 Bugtags SDK 后,测试人员就可直接在 App 里所见即所得的提交 Bug; SDK 会自动截屏 ...

  6. js汉字与拼音互转终极方案,附简单的JS拼音输入法【转】

    github项目地址:https://github.com/liuxianan/pinyinjs 完整demo演示:http://demo.liuxianan.com/pinyinjs/ 汉字转拼音: ...

  7. Leetcode Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  8. Graphviz从入门到不精通

    1.安装Graphviz (windows 版本,后面说linux下的安装) 1.1)下载安装文件 从graphviz官网下载 http://www.graphviz.org/Download.php ...

  9. java分享第七天-03(递归打印文件目录的树状结构)

    public static void main(String[] args) { File file= new File("e:/list"); printFile(file, 0 ...

  10. javascript循环和数组的基础练习

    九九乘法表 <script> //外层循环行数 for(var i=0; i<=9; i++){ //内曾循环控制每一行的列数 for(var j=0;j<=i; j++){ ...