Javaで再帰を使用して、指定された文字列のすべての可能な順列を計算しようとしています。しかし、自分のコードに何が問題なのか分かりません。文字列のすべての可能な順列を再帰的に計算するJava
public static ArrayList<String> computeAllPossiblePermutations(String str)
{
ArrayList<String> perms = new ArrayList<>();
//base case
if (str.length() == 1)
perms.add(str);
else
{
//loop over the string
for (int i = 0; i < str.length() - 1; i++)
{
//make a subset of the string excluding the first char
String sub = str.substring(i+1, str.length());
//compute permutations of the subset
ArrayList<String> subPerms = computeAllPossiblePermutations(sub);
//add the first char that we excluded at the start of each permutations
for (String s : subPerms)
{
s = str.charAt(i) + s;
perms.add(s);
}
}
}
return perms;
}
は、任意のヘルプをお願い申し上げ:
は、ここに私のアルゴリズムです。ありがとう!
。あなたが悪い結果を得ているなら、あなたが実際に得ているものと何が得られるかを示すべきです。 – Carcigenicate