LeetCode 155. Min Stack
2016-04-19 11:58
246 查看
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.
Perhaps, this is the most lazy way.....I will update other methods later.
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.
Perhaps, this is the most lazy way.....I will update other methods later.
#include <iostream> #include <stack> using namespace std; class MinStack{ private: struct value { int data; int currMin; }; stack<value> nodes; public: void push(int x) { if(nodes.empty()) { nodes.push({x, x}); } else { value tmp; tmp.data = x; tmp.currMin = min(x, nodes.top().currMin); nodes.push(tmp); } } void pop() { nodes.pop(); } int top() { return nodes.top().data; } int getMin() { return nodes.top().currMin; } }; int main(void) { MinStack myStack; myStack.push(1); myStack.push(2); cout << myStack.getMin() << endl; myStack.push(0); cout << myStack.getMin() << endl; }
相关文章推荐
- ios6新特性之下拉刷新UIRefreshControl
- ORACLE FOR UPDATE与FOR UPDATE OF区别
- Socket类的用法
- invalid constant type: 18
- 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
- 1、php文件上传保存。。。
- JavaWeb 项目——起点新闻社区
- 删除无用的系统服务项
- Linux中的编码转换程序convmv的安装和使用教程
- MySQL中字符串索引对update的影响分析
- vb作业
- 阅读Linux0.11——main.c
- Matlab标定工具箱使用的一些注意事项
- java Spring集合
- Centos6.5搭建Wiki
- 1026
- 读书笔记――javascript闭包
- 金格签章使用时碰到的奇葩问题解决
- poj 3041 Asteroids 最大匹配
- log4j2 配置 基于JAVA 以Web为例