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. 网络流24题-最长k可重线段集问题

    最长k可重线段集问题 时空限制1000ms / 128MB 题目描述 给定平面 x−O−y 上 n 个开线段组成的集合 I,和一个正整数 k .试设计一个算法,从开线段集合 I 中选取出开线段集合 S ...

  2. jump 转换进制+dp

    from Contest1024 - 省选模拟题14 题目大意 MMM站在x=0的地方,她想跳到x=t的地方.MMM每次跳跃可以选择跳到x - k或者x + k的地方,其中k={base^n | ba ...

  3. hdu 1754 线段树(点修改)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. Distinct Substrings(spoj 694)

    题意:要求不同子串的个数 /* 先求出height数组,不难看出height之和就是重复的字符串个数,用总的减去它就行了. */ #include<cstdio> #include< ...

  5. 【BZOJ1001】狼抓兔子(平面图最小割转最短路)

    题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路  1:(x,y)<==>(x+1,y ...

  6. 为了防止detailsview中修改后,而girdview却没立即更新显示

    原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] 可以在detailsview的事件中添加如下语句,即增加一个头,让它在0秒的时候刷新: Response.AddH ...

  7. How to build and run ARM Linux on QEMU from scratch

    This blog shows how to run ARM Linux on QEMU! This can be used as a base for later projects using th ...

  8. [深入学习C#]C#实现多线程的方式:Task——任务

    简介 .NET 4包含新名称空间System.Threading.Tasks,它 包含的类抽象出了线程功能. 在后台使用ThreadPool. 任务表示应完成的某个单元的工作. 这个单元的工作可以在单 ...

  9. hdu 4510(模拟)

    小Q系列故事——为什么时光不能倒流 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)T ...

  10. jquery鼠标点击窗口或浮动层以外关闭层【阻止冒泡事件】

    $(".up-list a.th1").click(function(){ $(this).next("ul#up-list-ul").show(); }); ...