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.

这道题神烦,总是超过内存限制,思路就是那个但是就是内存超了,不明觉历,拿到要用c数组?

总之需要注意两点:

1.每个操作都要O(1)

2.pop了以后注意min的变化

class MinStack {
private:
vector<int> min;
vector<int> data;
public:
void push(int x) {
data.push_back(x);
if (min.size() == || data[min.at(min.size()-)] > x) {
min.push_back((int)data.size()-);
}
} void pop() {
if (data.size()) {
if (min.size() > && min.at(min.size()-) == data.size()-) {
min.pop_back();
}
data.pop_back();
}
} int top() {
if (data.size() > )
return data.at(data.size()-);
return INT_MIN;
} int getMin() {
return data.at(min.at(min.size()-));
}
};

[LeetCode] Min Stack的更多相关文章

  1. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  2. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  3. [LeetCode] Min Stack 最小栈

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

  4. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

  5. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  6. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  7. [LeetCode] Min Stack 栈

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

  8. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  9. Min Stack [LeetCode 155]

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

随机推荐

  1. SQL 数据库初学笔记一

    做web刚好用得上SQL和php,图书馆借来书,来一个一晚上速成 <SQL必知必会>笔记 通用的语法,相关分类执行程序(DBMS): Apache Open Office Base Ado ...

  2. PyCharm 5 破解注册方法

    方法: 调整时间到2038年. 申请30天试用 退出pycharm 时间调整回来即可. 或者: 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 ...

  3. JVM(java 虚拟机)内存设置

    一.设置JVM内存设置 1. 设置JVM内存的参数有四个: -Xmx   Java Heap最大值,默认值为物理内存的1/4,最佳设值应该视物理内存大小及计算机内其他内存开销而定: -Xms   Ja ...

  4. 7.7---找只含3,5,7的数(CC150)

    ----思路:利用三个队列,一个存3,一个存5,一个存7. 然后,3*3的都放第一个.然后3*5,5*5的放第二个.然后,3*7,5*7,7*7的都放第三个. 答案: public static in ...

  5. SharePoint更改密码

    stsadm –o updatefarmcredentials –userlogin DomainName\UserName -password NewPassword –local  1. 通过管理 ...

  6. XsltListViewWebPart 和自定义列表视图

    http://msdn.microsoft.com/zh-cn/library/ff806162(v=office.14).aspx

  7. ios UIWindow 错误使用导致无法接收motionEnded(摇一摇)函数

    今天遇到一个问题,第一次运行程序时,- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event函数无法调用,第二次就好了 ...

  8. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  9. Java for LeetCode 209 Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  10. Django~Views

    In Django, web pages and other content are delivered by views. To get from a URL to a view, Django u ...