155. Min Stack

 

 

不是min就直接push一个min刀minStack

 

 1 class MinStack {
 2 
 3     /** initialize your data structure here. */
 4     Stack<Integer> stack, min;
 5     public MinStack() {
 6         stack = new Stack<>();
 7         min = new Stack<>();    
 8     }
 9     
10     public void push(int x) {
11         stack.push(x);
12         if(min.isEmpty() || x <= min.peek()){
13             min.push(x);
14         }else{
15             min.push(min.peek());
16         }
17         
18     }
19     
20     public void pop() {
21         stack.pop();
22         min.pop();
23         
24     }
25     
26     public int top() {
27         return stack.peek();
28         
29     }
30     
31     public int getMin() {
32         return min.peek();
33     }
34 }

 

posted @ 2018-10-06 09:17  jasoncool1  阅读(80)  评论(0编辑  收藏  举报