https://leetcode.com/problems/min-stack/

题目:

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.

思路:

关键在于找最小值要O(1)复杂度,所以空间换时间。一个额外的vector存储递减序入栈的数字。

AC代码:

 class MinStack {
public:
MinStack(){ st_min.push_back(INT_MAX); };
void push(int x) {
if (x <= st_min[st_min.size()-])
st_min.push_back(x);
st.push_back(x);
return;
} void pop() {
if (st[st.size() - ] == st_min[st_min.size() - ]){
st_min.pop_back();
}
st.pop_back();
return;
} int top() {
return st[st.size() - ];
} int getMin() {
return st_min[st_min.size() - ];
} private:
vector<int> st;
vector<int> st_min;
};

LeetCode(155)题解--Min Stack的更多相关文章

  1. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

  2. LeetCode(155) Min Stack

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

  3. LeetCode OJ:Min Stack(最小栈问题)

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

  4. LeetCode 155:最小栈 Min Stack

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

  5. Min Stack [LeetCode 155]

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

  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 155. Min Stack --------- java

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

  8. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  9. 155. Min Stack

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

随机推荐

  1. 【CF676C】Vasya and String(二分查找,线性扫描尺取法)

    题意: 给出一个长度为n的字符串,只有字符'a'和'b'.最多能改变k个字符,即把'a'变成'b'或把'b'变成'a'. 问改变后的最长连续相同字符的字串长度为多少. 首先是二分查找,好想也好写 .. ...

  2. LeetCode OJ--Gray Code **

    http://oj.leetcode.com/problems/gray-code/ 求格雷码的表示,主要应用递归. 递归生成码表 这种方法基于格雷码是反射码的事实,利用递归的如下规则来构造: 1位格 ...

  3. 富文本ZSSRichTextEditor之趟坑集锦

    富文本ZSSRichTextEditor是iOS原生与网页交互的集大成者,各种交互.自然问题也是多多,这篇文文章陆续更新遇到的奇葩问题. 1.问题1:从头条这种文章里头复制粘贴的文章,里边有图片,我们 ...

  4. 树讲解——牧场行走( lca )

    大视野   1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1947  Solved: 1021[Sub ...

  5. CDOJ 1171 两句话题意

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1171 题解: 这道题应该从gcd出来的值入手. 我们要求所有子集的gcd的和 首先我们先统计一下每个数字出现 ...

  6. jdk1.8中nashorn不支持ECMAScript6的问题

    背景 在项目中需要使用java调用javascript脚本,有两种方案,一种是faas,使用开源的openwhisk.另一种本地运行的方式,使用jdk的nashorn调用javascript,jdk版 ...

  7. Jackson对泛型的序列化和反序列化方法汇总

    说明:Jackson对于简单泛型是可以正常操作的,但是如果对于太过于复杂的泛型类有时会不成功.目前还在找着更合适的Json库.不过这一点在dotnet原生方案JavaScriptSerializer可 ...

  8. Understand the Business Domain

     Understand the Business Domain Mark Richards EFFECTivE SoFTWARE ARCHiTECTS understand not only tec ...

  9. vue Syntax Error: Unexpected token {

    > music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...

  10. hdu 5389 Zero Escape (dp)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②反复①直到得到的数仅仅有1位.给定n,A ...