zoukankan      html  css  js  c++  java
  • How to use multiple stacks to implement a dequeu (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 ImplementDequeUsingTwoStacks {
     6     private Deque<Integer> stackLeft;
     7     private Deque<Integer> stackRight ;
     8 
     9     public ImplementDequeUsingTwoStacks() {
    10         stackLeft = new LinkedList<>();
    11         stackRight = new LinkedList<>();
    12     }
    13     public void addLeft(int value){
    14         stackLeft.push(value);
    15     }
    16     public void addRight(int value){
    17         stackRight.push(value);
    18     }
    19     /*
    20         ->       <-
    21          1 2 | 3 4
    22         <-       ->
    23     this is very interesting: if left is not empty, return left
    24     if left is empty, then shuffle from right into left and then pop the left
    25     just remember that after each stack shuffle, the order is changed. thats exactly what we need: 3
    26     * */
    27     public Integer removeLeft(){
    28         return shuffleAndPop(stackRight, stackLeft);
    29     }
    30     public Integer removeRight(){
    31         return shuffleAndPop(stackLeft, stackRight);
    32     }
    33     /*
    34     * from: the stack items from
    35     * to: the stack items goes into and then pop
    36     * */
    37     private Integer shuffleAndPop(Deque<Integer> from , Deque<Integer> to){
    38         if (isEmpty()) return null;
    39         if (!to.isEmpty()){
    40             return to.pop();
    41         }
    42         else{
    43             while (!from.isEmpty()){
    44                 to.push(from.pop());
    45             }
    46             return to.pop();
    47         }
    48     }
    49 
    50     public boolean isEmpty(){
    51         return stackLeft.isEmpty() && stackRight.isEmpty() ;
    52     }
    53     //lazy
    54     public int size(){
    55         return stackLeft.size() + stackRight.size() ;
    56     }
    57 
    58     public static void main(String[] args) {
    59         ImplementDequeUsingTwoStacks deque = new ImplementDequeUsingTwoStacks();
    60         //1 2 | 3 4
    61         deque.addLeft(2);
    62         deque.addRight(3);
    63         deque.addRight(4);
    64         deque.addLeft(1);
    65         System.out.println(deque.removeLeft());//1
    66         System.out.println(deque.removeRight());//4
    67         System.out.println(deque.removeRight());//3
    68         System.out.println(deque.size()); //1
    69         System.out.println(deque.removeLeft()); //2
    70         System.out.println(deque.size()); //0
    71     }
    72 }

    个人感想: 这并没有优化太多,因为SHUFFLE 把整个都搬过去另一边,

    WORST CASE 的情况是 “雨露均沾”: 当你SHUFFLE 从左边到右边的时候, 马上REMOVELEFT, 那你就得马上再从右边SHUFFLE 到左边。。。 和捉迷藏一样,每次POP 都是O(N) 所以是很糟糕的一个算法。 

    最好的结果: 专宠一人。  时间复杂度可以AMORTIZE 近似为O(1)

    优化版本 using three stacks 在这里

  • 相关阅读:
    7.18学习日志
    7.16学习日志
    5 Things They Never Tell You About Making iPhone Apps
    MantisBT
    25款实用的桌面版博客编辑器
    【转】如何学会600多种编程语言
    开发者如何提升和推销自己
    CleanMyMac 1.10.8
    VMWARE FUSION 6 KEY
    cocos2dx shader
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8614799.html
Copyright © 2011-2022 走看看