leetcode Min Stack
2015-12-09 20:02
274 查看
原题链接:https://leetcode.com/problems/min-stack/
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.
Description
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) { if (val.empty() || x <= val.top()) val.push(x); num.push(x); } void pop() { if (num.top() <= val.top()) val.pop(); num.pop(); } int top() { return num.top(); } int getMin() { return val.top(); } private: stack<int> num; stack<int> val; };
相关文章推荐
- 1191: [HNOI2006]超级英雄Hero
- wordCount的认识(java版)
- SAP接口编程-RFC系列04 : 动态调用SAP函数
- 关于LinkedList的操作--重点
- 几个java代码优化注意点
- BZOJ1857: [Scoi2010]传送带
- Unity 协程与线程区别
- Activiti进阶(三)——流程定义的CRUD
- html返回上一页
- int 与String之间的转换
- jquery特效 点击展示与隐藏全文
- 我就是想看一下博客多久能显示出来
- android JNI常用添加log方法
- Java排序之排序大综合
- 关于 iOS 的 StoryBoard,接受的那一刻才发现她的美 - 当然美的事物都需要业心照料
- Oracle
- PHP 连接 oracle数据库(可远程)的测试操作
- Oracle导出Excel时怎么避免出现添加引号、字段前面去0等问题
- TortoiseSVN 命令 (命令行执行工具)
- MyEclipse与heidiMysql