簡體   English   中英

找出一組數字的所有可能組合

[英]Find all possible combinations of a set of numbers

如何在 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]);

                                }
                            }
                        }
                    }
                }
            }
        }

    }

}

看起來您正在嘗試為 k = “集合”的長度找到長度為 1 到 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]

這可以適用於采用集合而不是字符串,但應該讓您繼續前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM