順列を再帰的に計算するこのアルゴリズムの時間的複雑さはO(n!*n)
であるべきですが、私は空間の複雑さについて100%確信していません。この順列アルゴリズムの空間複雑度はどのくらいですか?
はn
再帰あり、そして再帰のために必要な最大のスペースはすべての順列*順列のn!
(数)のn
(スペースです。宇宙algorithm` Oの複雑さ(N!* N^2)ですか?
static List<String> permutations(String word) {
if (word.length() == 1)
return Arrays.asList(word);
String firstCharacter = word.substring(0, 1);
String rest = word.substring(1);
List<String> permutationsOfRest = permutations(rest);
List<String> permutations = new ArrayList<String>(); //or hashset if I don’t want duplicates
for (String permutationOfRest : permutationsOfRest) {
for (int i = 0; i <= permutationOfRest.length(); i++) {
permutations.add(permutationOfRest.substring(0, i) + firstCharacter + permutationOfRest.substring(i));
}
}
return permutations;
}
n! O(怖い)です:) –