查找一组数字的所有可能组合

时间:2015-06-24 20:21:56

标签: java combinations

如何在Java中找到一组数字的所有可能组合,例如:

My Set: 1,2,3,4,5

My Output: 1, 2, 3, 4, 5, 12, 13, 14...145, 543 etc

我有这个代码,我在研究stackoverflow时发现:

private static void permutation(String prefix, String str) {
        int n = str.length();
        if (n == 0) {
            System.out.println(prefix);
        } else {
            for (int i = 0; i < n; i++) {
                permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
            }
        }
    }

但忽略的是你可以将12作为代码。有人能指出我正确的方向吗?我可以自己写一个,但它可能会很慢,我希望能够尽快执行它。提前致谢

我的弱尝试:(因为我只需要6或8位的组合)

public class NumberCombo {

    public static void main(String[] args) {
        int[] combo = new int[8];
        for (int i = 0; i < combo.length; i++) {
            combo[i] = i + 1;
        }

        for (int i = 0; i < combo.length; i++) {
            System.out.println(combo[i]);
            for (int x = 0; x < combo.length; x++) {
                System.out.println(combo[i] + "" + combo[x]);
                for (int y = 0; y < combo.length; y++) {
                    System.out.println(combo[i] + "" + combo[x] + "" + combo[y]);
                    for (int z = 0; z < combo.length; z++) {
                        System.out.println(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z]);
                        for (int z1 = 0; z1 < combo.length; z1++) {
                            System.out.println(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z]+""+combo[z1]);
                            for (int z2 = 0; z2 < combo.length; z2++) {
                            System.out.println(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z]+""+combo[z1]+""+combo[z2]);
                                for (int z3 = 0; z3 < combo.length; z3++) {
                                    System.out.println(combo[i] + "" + combo[x] + "" + combo[y] + "" + combo[z]+""+combo[z1]+""+combo[z3]);

                                }
                            }
                        }
                    }
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

看起来你试图找到长度为1到k的排列,k =你的“集合”的长度:

  public List<String> permutations(String prefix, String str, int k) {
    if (prefix.length() == k) {
      return Collections.singletonList(prefix);
    }
    List<String> results = new ArrayList<>();
    for (int i = 0; i < str.length(); i++) {
      results.addAll(permutations(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, str.length()), k));
    }
    return results;
  }

  public List<String> allLengthPermutations(String s) {
    List<String> results = new ArrayList<>();
    for (int i = 1; i <= s.length(); i++) {
      results.addAll(permutations("", s, i));
    }
    return results;
  }

  @Test
  public void allLengthPermutationsTest() {
    System.out.println(allLengthPermutations("1234"));
  }
// [1, 2, 3, 4, 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, 43, 123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432, 1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321]

这可以适用于收集而不是字符串,但应该让你上路。