栈————最小栈

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

 

posted @ 2019-06-15 18:58  Austin_anheqiao  阅读(148)  评论(0编辑  收藏  举报