牛客网-《剑指offer》-包含min函数的栈

题目:http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

辅助栈

C++

 1 class Solution {
 2 public:
 3     void push(int v) {
 4         int m = s1.empty() ? v : this->min();
 5         m = m < v ? m : v;
 6         s1.push(v);
 7         s2.push(m);
 8     }
 9     void pop() {
10         s1.pop();
11         s2.pop();
12     }
13     int top() {
14         return s1.top();
15     }
16     int min() {
17         return s2.top();
18     }
19 private:
20     stack<int> s1;
21     stack<int> s2;
22 };

 

你可能感兴趣的:(牛客网-《剑指offer》-包含min函数的栈)