码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] 带最小值操作的栈

时间:2015-06-28 16:56:51      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 1 class MinStack {
 2 public:
 3     MinStack() {
 4         // do initialization if necessary
 5     }
 6 
 7     void push(int number) {
 8         // write your code here
 9         if (minimum.empty() || number <= minimum.top())
10             minimum.push(number);
11         data.push(number);
12     }
13 
14     int pop() {
15         // write your code here
16         if (minimum.top() == data.top())
17             minimum.pop();
18         int elem = data.top();
19         data.pop();
20         return elem;
21     }
22 
23     int min() {
24         // write your code here
25         return minimum.top();
26     }
27 private:
28     stack<int> data;
29     stack<int> minimum;
30 };

 

[LintCode] 带最小值操作的栈

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4605603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!