[LintCode] 带最小值操作的栈

 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 };

 

posted @ 2015-06-28 15:21  jianchao-li  阅读(453)  评论(0编辑  收藏  举报