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 };
本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5113175.html,如需转载请自行联系原作者