给定字符串,打印它的所有排列。为此,我想出了以下程序。
public static char[] swap(char[] input, int i, int j) {
char temp;
temp = input[i];
input[i] = input[j];
input[j] = temp;
return input;
}
/**
*
* @param args
*/
public static void permuteStrings(char[] inputString, int start, int finish ) {
//Base case: When there is only single element, print the string
if(start == finish)
System.out.println(inputString);
else {
//Recursive case: Swap first element with all the elements and permute on the
// rest of string.
for(int i = start; i <= finish; i++) {
inputString = swap(inputString, start, i);
permuteStrings(inputString, i + 1, finish);
inputString = swap(inputString,start, i); //restoring the original string
}
}
}
但是,对于给定的输入 ABC,它打印的所有内容都是
ABC
BAC
我似乎无法弄清楚问题是什么