Java 程序中的重复排列
Duplicate permutations in Java program
我阅读了递归生成字符串排列的算法。
invoke the function with j = 1
if (j == length of string)
print the string and return
else
for (i = j to length of string)
interchange jth character with ith character
call function on j + 1
我使用 java 实现了如下:
class PERMUTATION {
private int count = 1;
private char[] arr = {'A', 'B', 'C'};
public void perm(int k) {
if (k == 3) {
System.out.print(count+++".");
for (int i = 0; i < 3; ++i)
System.out.print(arr[i]+" ");
System.out.println();
return;
}
for (int i = k; i <= 3; ++i) {
/*interchanging ith character with kth character*/
char c = arr[i - 1];
arr[i - 1] = arr[k - 1];
arr[k - 1] = c;
perm(k + 1);
}
}
public static void main(String []args) {
System.out.println("the permutations are");
PERMUTATION obh=new PERMUTATION();
obh.perm(1);
}
}
但是我的程序正在生成重复的排列。为什么?
如果 "source" 数组保持不变,则此算法有效,因此每个索引都会被正确处理。
让我们看看您的代码的输出:
1.A B C
2.A C B
3.C A B
4.C B A
5.A B C
6.A C B
如您所见,在第 1 次迭代中。 3,它应该移动 B 到第一个索引,你的移动 C 相反,因为你已经移动了 B 到不同的位置。
由于这个事实,B 没有机会进入第一个索引,只会 "bounce" 在 2 和 3 之间。
您的主要问题是,您正在更改 "source" 数组。如果你避免这种情况,那么你的算法就可以正常工作:
class PERMUTATION {
private int count = 1;
public void perm(char[] arr, int k) {
if (k == 3) {
System.out.print(count++ + ".");
for (int i = 0; i < 3; ++i)
System.out.print(arr[i] + " ");
System.out.println();
return;
}
char[] arr2 = arr.clone(); // clone the passed array, so we don't mess it up
for (int i = k; i <= 3; ++i) {
/* interchanging ith character with kth character */
char c = arr2[k - 1];
arr2[k - 1] = arr2[i - 1];
arr2[i - 1] = c;
perm(arr2, k + 1);
}
}
public static void main(String[] args) {
System.out.println("the permutations are");
PERMUTATION obh = new PERMUTATION();
obh.perm(new char[] {'A', 'B', 'C'}, 1); // pass the original array
}
}
输出将是:
1.A B C
2.A C B
3.B A C
4.B C A
5.C A B
6.C B A
顺便说一句:请注意 Java Naming Conventions,所以不要调用 class PERMUTATION
,而是使用 Permutation
。
我阅读了递归生成字符串排列的算法。
invoke the function with j = 1 if (j == length of string) print the string and return else for (i = j to length of string) interchange jth character with ith character call function on j + 1
我使用 java 实现了如下:
class PERMUTATION {
private int count = 1;
private char[] arr = {'A', 'B', 'C'};
public void perm(int k) {
if (k == 3) {
System.out.print(count+++".");
for (int i = 0; i < 3; ++i)
System.out.print(arr[i]+" ");
System.out.println();
return;
}
for (int i = k; i <= 3; ++i) {
/*interchanging ith character with kth character*/
char c = arr[i - 1];
arr[i - 1] = arr[k - 1];
arr[k - 1] = c;
perm(k + 1);
}
}
public static void main(String []args) {
System.out.println("the permutations are");
PERMUTATION obh=new PERMUTATION();
obh.perm(1);
}
}
但是我的程序正在生成重复的排列。为什么?
如果 "source" 数组保持不变,则此算法有效,因此每个索引都会被正确处理。
让我们看看您的代码的输出:
1.A B C
2.A C B
3.C A B
4.C B A
5.A B C
6.A C B
如您所见,在第 1 次迭代中。 3,它应该移动 B 到第一个索引,你的移动 C 相反,因为你已经移动了 B 到不同的位置。 由于这个事实,B 没有机会进入第一个索引,只会 "bounce" 在 2 和 3 之间。
您的主要问题是,您正在更改 "source" 数组。如果你避免这种情况,那么你的算法就可以正常工作:
class PERMUTATION {
private int count = 1;
public void perm(char[] arr, int k) {
if (k == 3) {
System.out.print(count++ + ".");
for (int i = 0; i < 3; ++i)
System.out.print(arr[i] + " ");
System.out.println();
return;
}
char[] arr2 = arr.clone(); // clone the passed array, so we don't mess it up
for (int i = k; i <= 3; ++i) {
/* interchanging ith character with kth character */
char c = arr2[k - 1];
arr2[k - 1] = arr2[i - 1];
arr2[i - 1] = c;
perm(arr2, k + 1);
}
}
public static void main(String[] args) {
System.out.println("the permutations are");
PERMUTATION obh = new PERMUTATION();
obh.perm(new char[] {'A', 'B', 'C'}, 1); // pass the original array
}
}
输出将是:
1.A B C
2.A C B
3.B A C
4.B C A
5.C A B
6.C B A
顺便说一句:请注意 Java Naming Conventions,所以不要调用 class PERMUTATION
,而是使用 Permutation
。