1 class MinStack { 2 public: 3 /** initialize your data structure here. */ 4 stack<int> stk, stk_min; //定义 5 MinStack() { 6 7 } 8 9 void push(int x) { 10 stk.push(x); 11 if (stk_min.size()) { 12 x = min(x, stk_min.top()); 13 } 14 stk_min.push(x); 15 //cout << stk.top() << " " << stk_min.top() << endl; 16 } 17 18 void pop() { 19 if (stk.size()) { 20 stk.pop(); 21 } 22 if (stk_min.size()) { 23 stk_min.pop(); 24 } 25 } 26 27 int top() { 28 return stk.top(); 29 } 30 31 int getMin() { 32 return stk_min.top(); 33 } 34 }; 35 36 /** 37 * Your MinStack object will be instantiated and called as such: 38 * MinStack obj = new MinStack(); 39 * obj.push(x); 40 * obj.pop(); 41 * int param_3 = obj.top(); 42 * int param_4 = obj.getMin(); 43 */