简体   繁体   English

如何创建所有可能的排列,包括重复?

[英]How to create all possible permutations including duplicates?

I wish to create all permutations of all digits from a function.我希望从一个函数中创建所有数字的所有排列。

For eg I have the array [1,2,3].例如,我有数组 [1,2,3]。 I would like the function to first print all possible permutations of 1 digit numbers:我希望该函数首先打印 1 位数字的所有可能排列:

1
2
3

then all possible permutations of 2-digit numbers(duplicates like 11, 22, 33 included)然后是 2 位数字的所有可能排列(包括 11、22、33 等重复项)

11
12
13
21
22
23
31
32
33

and similarly for the 3 digit numbers.同样适用于 3 位数字。

I want to include all sort of possible permutations(11, 111, 222 etc.) up to the number of digits in the array, like in this example, I would like to have all permutations up to 3 digits.我想包括所有可能的排列(11、111、222 等),直到数组中的位数,就像在这个例子中一样,我希望所有排列最多 3 位。 How can I write it?我该怎么写?

My current code creates only exclusive permutations of only the number of digits asked, not the smaller digits:我当前的代码仅创建仅询问的位数的排他排列,而不是较小的位数:

public class Permutation {
    public static void main(String[] args)
    {
        String str = "132546";
        int n = str.length();
        Permutation permutation = new Permutation();

        permutation.permute(str, 0, n-1);

    }

    private void permute(String str, int l, int r)
    {
        if (l == r)
            System.out.println(str);
        else
        {
            for (int i = l; i <= r; i++)
            {
                str = swap(str,l,i);
                permute(str, l+1, r);
                str = swap(str,l,i);
            }
        }
    }


    public String swap(String a, int i, int j)
    {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }

}

I want it to create all permutations as written above.我希望它创建上面写的所有排列。

Try this:尝试这个:

import java.util.Arrays;

class Permutation {

    public static void main(String[] args){
        String str = "123456";
    
        Permutation permutation = new Permutation();

        permutation.printAll(str);
    
    }

    private void printAll(String str) {
        int n = str.length();
        for(int k=1;k<=n;k++) {//k - number of digits
            System.out.println("=================");
            System.out.println(k + " digit(s):");
            printPermutationsWithRepet(str,k);
        }
    }

    private void printPermutationsWithRepet(String str, int k) {
        String [] a =new String [k];
        printPermutations(str,k,0,1, a);
    }
    
    private void printPermutations(String str, int k, int startPosition, int maxUsed, String [] a) {    
        if(startPosition == k) {
            System.out.println(Arrays.toString(a));
        } else {
            for(int i = maxUsed; i <= str.length(); i++) {
                a[startPosition] = str.charAt(i-1)+"";
                printPermutations(str,k,startPosition+1,i, a);
            }
        }
    }  
}

Note that the way the digits change is like the odometer on a car (but with funky non-standard digits).请注意,数字变化的方式就像汽车上的里程表(但带有时髦的非标准数字)。 So one way to go is "simulate" the odometer.所以一种方法是“模拟”里程表。 No fancy recursion needed.不需要花哨的递归。

class Odometer {
  private final int[] digits = {1, 2, 3};
  /** Indices into the digits array. Initially all zero. */
  private final int[] indices = new int[2];

  /** Advance to the next value, returning whether it's a non-repeat. */
  boolean next() {
    for (int i = 0; i < indices.length; ++i) {
      // If incrementing the current digit index causes it to "wrap."
      if (++indices[i] == digits.length) {      
        indices[i] = 0;  // It wrapped. Reset this index and keep going.
      } else {        
        return true;  // Otherwise it didn't wrap. We're done.
      }
    }
    return false;  // All indices have wrapped back to zero.
  }

  /** Print current digits. */
  void print() {
    for (int i = 0; i < indices.length; ++i) {
      System.out.print(digits[indices[i]]);
    }
    System.out.println();
  }

  public static void main(String[] args) {
    Odometer odo = new Odometer();
    do {
      odo.print();
    } while (odo.next());
  }
}

A more general comment is that this isn't a permutation problem.更一般的评论是,这不是排列问题。 It appears your initial solution is based on that mistake.您最初的解决方案似乎是基于该错误。 The code is for the all permutations problem.该代码适用于所有排列问题。 Consequently it's not close to correct.因此,它并不接近正确。 If that's the result of Googling for "permutations" and copy/pasting code you didn't understand, pause and take stock.如果这是谷歌搜索“排列”和复制/粘贴您不理解的代码的结果,请暂停并盘点。 This is not a useful strategy.这不是一个有用的策略。 It will end badly.结局会很糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM