zoukankan      html  css  js  c++  java
  • Three Stacks implements a Deque

     1 import java.util.*;
     2 
     3 public class TwoStackImplementDeque {
     4     public static void main(String[] args) {
     5         DequeStructure d = new DequeStructure();
     6         d.leftPush(4);
     7         d.leftPush(3);
     8         d.leftPush(2);
     9         d.leftPush(1);
    10         d.rightPush(9);
    11         d.rightPush(8);
    12         d.rightPush(7);
    13         d.rightPush(6);
    14         System.out.println(d.leftPop());
    15         System.out.println(d.leftPop());
    16         System.out.println(d.leftPop());
    17         System.out.println(d.leftPop());
    18         System.out.println(d.leftPop());
    19         System.out.println(d.leftPop());
    20         System.out.println(d.leftPop());
    21         System.out.println(d.leftPop());
    22     }
    23 }
    24 
    25 class DequeStructure {
    26     Deque<Integer> stack1;
    27     Deque<Integer> stack2;
    28     Deque<Integer> stack3;
    29     public DequeStructure () {
    30         stack1 = new LinkedList<>();
    31         stack2 = new LinkedList<>();
    32         stack3 = new LinkedList<>();
    33     }
    34 
    35     public void leftPush(int x) {
    36         stack1.push(x);
    37     }
    38 
    39     public void rightPush(int x) {
    40         stack2.push(x);
    41     }
    42 
    43     public int leftPop() {
    44         if (stack1.isEmpty() && stack2.isEmpty()) {
    45             return -1;
    46         }
    47         if (stack1.isEmpty()){
    48             shuffle();
    49         }
    50         return stack1.pop();
    51     }
    52 
    53     public int rightPop() {
    54         if (stack1.isEmpty() && stack2.isEmpty()) {
    55             return -1;
    56         }
    57         if (stack2.isEmpty()) {
    58             shuffle();
    59         }
    60         return stack2.pop();
    61     }
    62     
    63     private void shuffle() {
    64         //用两个指针,可以省去写多余的代码块!!!
    65         Deque<Integer> nonEmpty = stack1;
    66         Deque<Integer> empty = stack2;
    67         if (stack1.isEmpty()) {
    68             nonEmpty = stack2;
    69             empty = stack1;
    70         }
    71 
    72         int halfSize = nonEmpty.size() / 2;
    73         for (int i = 0; i < halfSize; i++) {
    74             stack3.push(nonEmpty.pop());
    75         }
    76         while (!nonEmpty.isEmpty()) {
    77             empty.push(nonEmpty.pop());
    78         }
    79 
    80         while (!stack3.isEmpty()) {
    81             nonEmpty.push(stack3.pop());
    82         }
    83     }
    84 }
  • 相关阅读:
    [LeetCode] 1190. Reverse Substrings Between Each Pair of Parentheses
    [LeetCode] 923. 3Sum With Multiplicity
    [LeetCode] 643. Maximum Average Subarray I
    [LeetCode] 669. Trim a Binary Search Tree
    [LeetCode] 1743. Restore the Array From Adjacent Pairs
    [LeetCode] 888. Fair Candy Swap
    [LeetCode] 1102. Path With Maximum Minimum Value
    [LeetCode] 1631. Path With Minimum Effort
    [LeetCode] 1522. Diameter of N-Ary Tree
    [LeetCode] 1376. Time Needed to Inform All Employees
  • 原文地址:https://www.cnblogs.com/mayinmiao/p/8606852.html
Copyright © 2011-2022 走看看