LeetCode: Min Stack
来源:互联网 发布:阿里云 ecs 是iaas 编辑:程序博客网 时间:2024/05/23 12:04
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
class MinStack {public: void push(int x) { m_stack.push(x); if(min_stack.empty()) min_stack.push(x); else { int min = min_stack.top(); if(x <= min) min_stack.push(x); } } void pop() { if(m_stack.top() == min_stack.top()) min_stack.pop(); m_stack.pop(); } int top() { return m_stack.top(); } int getMin() { return min_stack.top(); }private: stack<int> m_stack; stack<int> min_stack;};
Round2:
class MinStack {public: void push(int x) {m_stack.push(x);if((min.size() > 0 && x <= min.top()) || min.size() == 0)min.push(x); } void pop() {if(m_stack.top() == min.top())min.pop();m_stack.pop(); } int top() {return m_stack.top(); } int getMin() {return min.top(); }private:stack<int> m_stack;stack<int> min;};
Round 3:
class MinStack {public: void push(int x) { m_stack.push(x); if(m_minStack.empty() || m_minStack.top() >= x) m_minStack.push(x); } void pop() { if(m_minStack.top() == m_stack.top()) m_minStack.pop(); m_stack.pop(); } int top() { return m_stack.top(); } int getMin() { return m_minStack.top(); }private: stack<int> m_stack; stack<int> m_minStack;};
0 0
- LeetCode[stack]: Min Stack
- [leetcode][stack] Min Stack
- 【Leetcode】Min Stack
- Leetcode: Min Stack
- leetcode Min stack
- Min Stack -- LeetCode
- Leetcode: Min Stack
- 【LeetCode】Min Stack
- leetcode Min Stack
- leetcode--Min Stack
- [Leetcode]Min stack
- leetcode:Min Stack
- leetcode 之 Min Stack
- leetcode Min Stack
- LeetCode(155) Min Stack
- [LeetCode]Min Stack
- leetcode:Min Stack
- leetcode - Min Stack
- dede开发网上报名简单示例
- Android Bluetooth
- Android SDK版本号与API级别的对应关系以及创建工程时选择API版本的注意事项
- 在IIS(64位)上部署WCF服务访问Oracle数据库
- MYSQL数据库查询保留两位小数
- LeetCode: Min Stack
- jquery 元素控制(追加元素/追加内容)介绍及应用
- 解决ubuntu系统udev多网卡名称变化的问题
- Yii cookie session 操作
- LVS与常用Web服务器软件的特性对比
- 网站
- 在O(1)时间删除链表结点
- C#.NET 实现右下角弹出窗口
- 第8周 项目1-分段函数求值