【问题标题】:Permutations of Strings in an Array need to be in same order but aren't数组中字符串的排列需要以相同的顺序排列,但不是
【发布时间】:2021-02-02 08:03:12
【问题描述】:

使用递归来打印 ArrayList 中名称字符串的所有排列,需要它们按特定顺序排列,有些行是正确的,然后有些行在切换排列的第一个单词的末尾时会混乱。例如索引,它需要是 3,1,2 然后是 3,2,1 但是它是相反的。

我的输出:

  • 朱莉娅·卢卡斯·米娅
  • 朱莉娅·米娅·卢卡斯
  • 卢卡斯·朱莉娅·米娅
  • 卢卡斯·米娅·朱莉娅
  • 米娅·卢卡斯·朱莉娅
  • 米娅·朱莉娅·卢卡斯

预期输出:

  • 朱莉娅·卢卡斯·米娅
  • 朱莉娅·米娅·卢卡斯
  • 卢卡斯·朱莉娅·米娅
  • 卢卡斯·米娅·朱莉娅
  • 米娅·朱莉娅·卢卡斯
  • Mia Lucas Julia(上面一行需要交换)
   public static void allPermutations(ArrayList<String> permList, ArrayList<String> nameList, 
   int index) {
      
      if (index == nameList.size() - 1) {
         for (int i = 0; i < nameList.size(); i++) {
            permList.add(nameList.get(i));
         }
         for (String val: permList) {
            System.out.print(val + " ");
         }
         System.out.println();
         permList.clear();
         
      } 
      for (int i = index; i < nameList.size(); i++) {
         Collections.swap(nameList, index, i);
         allPermutations(permList, nameList, index + 1);
         Collections.swap(nameList, index, i);
      }
         
      }
      
   }

   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      ArrayList<String> nameList = new ArrayList<String>();
      ArrayList<String> permList = new ArrayList<String>();

      nameList.add("Julia");
      nameList.add("Lucas");
      nameList.add("Mia ");
      
      
      allPermutations(permList, nameList, 0);
      
   }

【问题讨论】:

    标签: java recursion permutation


    【解决方案1】:

    假设

    1. 允许迭代代码

    逻辑

    1. 初始化字符串数组和对应的整数数组以保持原始元素的顺序
    2. 迭代计算下一个排列

    下一个排列

    1. 以相反的顺序扫描给定的输入
    2. 找到value[pivot] &lt; value[pivot + 1] 所在的枢轴索引
    3. 枢轴右侧的所有条目均按降序排列
    4. 对于给定的排列,下一个排列将是立即更大的值(如果从数字上考虑)
    5. 例如:1234 -> 1243 -> 1324 -> 1342
    6. 找到刚好比枢轴元素大的元素(在右侧)
    7. 交换这两个元素
    8. 枢轴索引右侧的所有元素仍然按降序排列
    9. 将元素从pivot + 1 反转为end(升序排列)
    10. 这是下一个排列

    代码

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Permutation {
    
        // iteratively compute all permutaions
        private static void permute(String[] permutation, Integer[] order) {
            boolean hasMore;
            do {
                System.out.println(Arrays.deepToString(permutation));
                hasMore = next(permutation, order);
            } while (hasMore);
        }
    
        // given a permutation, find the next permutation based on the order
        private static boolean next(String[] permutation, Integer[] order) {
            int pivot = order.length - 2;
            // check from last and find the first left index greater than its next right index
            while (pivot >= 0 && order[pivot + 1] < order[pivot]) {
                pivot--;
            }
    
            // if reached beginning, then completed
            if (pivot < 0) {
                return false;
            }
    
            // find the index of least bigger element on the right
            int swap = pivot + 1;
            while (swap < order.length && order[pivot] < order[swap]) {
                swap++;
            }
            swap--;
    
            // swap pivot and its least bigger elemment
            swap(order, swap, pivot);
            swap(permutation, swap, pivot);
    
            // reverse the descending part after pivot and turn into ascending
            reverse(order, pivot + 1, order.length - 1);
            reverse(permutation, pivot + 1, permutation.length - 1);
    
            return true;
        }
    
        private static <T> void reverse(T[] a, int left, int right) {
            while (left < right) {
                swap(a, left++, right--);
            }
        }
    
        private static <T> void swap(T[] a, int i, int j) {
            T temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    
        public static void main(String[] args) {
            List<String> nameList = new ArrayList<>();
    
            nameList.add("Julia");
            nameList.add("Lucas");
            nameList.add("Mia");
    
            String[] permutation = nameList.toArray(new String[nameList.size()]);
            Integer[] order = IntStream.range(0, nameList.size()).boxed()
                .collect(Collectors.toList()).toArray(new Integer[nameList.size()]);
            permute(permutation, order);
        }
    }
    
    

    【讨论】:

    • 我就是喜欢这个家伙的回答方式
    • 谢谢@aran
    【解决方案2】:

    如果您想要显示的预期答案,那么您可以尝试我的代码 ->

    static void permutate(ArrayList<String> ar, String ans, boolean b[], int v) {
        if (v == 3) {
            System.out.println(ans);
            return;
        }
        for (int i = 0; i < ar.size(); i++) {
            if (b[i] == false) {
                b[i] = true;
                permutate(ar, ans + ar.get(i) + "  ", b, v + 1);
                b[i] = false;
            }
        }
    }
    
    public static void main(String[] args) {
        ArrayList<String> ar = new ArrayList<>();
        ar.add("Julia ");
        ar.add("Lucas ");
        ar.add("Mia ");
        permutate(ar, "", new boolean[4], 0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      相关资源
      最近更新 更多