How to use multiple stacks to implement a dequeu (using three stacks)

 

这是 Implement Deque Using Two Stacks 的升华版本, 存一半有效的把时间复杂度最坏情况降低到O(N/2) 

 这里最好,最坏的情况正好和  Implement Deque Using Two Stacks 相反。

最好的情况: 雨露均沾

最坏的情况:专宠一人

  1 /*
  2 dequeu can add/remove from two sides
  3 stack can add/remove from only one side
  4 * */
  5 public class ImplementDequeUsingThreeStacks {
  6     private Deque<Integer> stackLeft;
  7     private Deque<Integer> stackRight ;
  8     private Deque<Integer> stackBuffer;
  9 
 10     public ImplementDequeUsingThreeStacks() {
 11         stackLeft = new LinkedList<>();
 12         stackRight = new LinkedList<>();
 13         stackBuffer = new LinkedList<>();
 14     }
 15     public void addLeft(int value){
 16         stackLeft.push(value);
 17     }
 18     public void addRight(int value){
 19         stackRight.push(value);
 20     }
 21     /*
 22         ->       <-
 23          1 2 | 3 4
 24         <-       ->
 25     this is very interesting: if left is not empty, return left
 26     if left is empty, then:
 27      1) shuffle the first 1/2 right into the buffer to host,
 28      2) and then shift the 2nd half into the left
 29      3) pop from the left
 30 
 31     just remember that after each stack shuffle, the order is changed. thats exactly what we need: 3
 32     * */
 33     //     | 3 4
 34     public Integer removeLeft(){
 35         if (!stackLeft.isEmpty()){
 36             return stackLeft.pop();
 37         }
 38         //corner case
 39         if (isEmpty()){
 40             return null;
 41         } else{
 42             //left:[] buffer:[] right:[3,4]
 43             int mid = stackRight.size()/2 ;
 44             // < since its index based
 45             // left:[]   buffer: [4] right:[3]
 46             for (int i = 0; i <mid ; i++) {
 47                 stackBuffer.push(stackRight.pop());
 48             }
 49             // left:[3] buffer: [4] right:[]
 50             while (!stackRight.isEmpty()){
 51                 stackLeft.push(stackRight.pop());
 52             }
 53             //left:[3] buffer: [] right:[4]
 54             while (!stackBuffer.isEmpty()){
 55                 stackRight.push(stackBuffer.pop());
 56             }
 57             return stackLeft.pop();
 58         }
 59     }
 60     // 1 2 |
 61     public Integer removeRight(){
 62         if (!stackRight.isEmpty()){
 63             return stackRight.pop();
 64         }
 65         //corner case
 66         if (isEmpty()){
 67             return null;
 68         } else{
 69             // left:[1,2] buffer:[] right:[]
 70             int mid = stackRight.size()/2 ;
 71             // < since its index based
 72             // left:[2]   buffer: [1] right:[]
 73             for (int i = 0; i <mid ; i++) {
 74                 stackBuffer.push(stackLeft.pop());
 75             }
 76             // left:[] buffer: [1] right:[2]
 77             while (!stackRight.isEmpty()){
 78                 stackRight.push(stackLeft.pop());
 79             }
 80             //left:[1] buffer: [] right:[2]
 81             while (!stackBuffer.isEmpty()){
 82                 stackLeft.push(stackBuffer.pop());
 83             }
 84             return stackRight.pop();
 85         }
 86     }
 87 
 88 
 89     public boolean isEmpty(){
 90         return stackLeft.isEmpty() && stackRight.isEmpty() ;
 91     }
 92     //lazy
 93     public int size(){
 94         return stackLeft.size() + stackRight.size() ;
 95     }
 96 
 97     public static void main(String[] args) {
 98         ImplementDequeUsingThreeStacks deque = new ImplementDequeUsingThreeStacks();
 99         //1 2 | 3 4
100         deque.addLeft(2);
101         deque.addRight(3);
102         deque.addRight(4);
103         deque.addLeft(1);
104         System.out.println(deque.removeLeft());//1
105         System.out.println(deque.removeRight());//4
106         System.out.println(deque.removeRight());//3
107         System.out.println(deque.size()); //1
108         System.out.println(deque.removeLeft()); //2
109         System.out.println(deque.size()); //0
110     }
111 }

 

 

posted @ 2018-03-21 05:04  davidnyc  阅读(280)  评论(0编辑  收藏  举报