字符串全排列
public class 字符串全排列
{
public static void allSortOfStr(char[] arr, int begin, int end)
{
if (begin == end)
{
// 只有一个字符进行全排序时,进行输出
for (int i = 0; i <= end; i++)
{
System.out.print(arr[i]);
}
System.out.println();
}
else
{
for (int i = begin; i <= end; i++)
{
// 交换字符
char tempChar = arr[i];
arr[i] = arr[begin];
arr[begin] = tempChar;
// 全排列后续的字符
allSortOfStr(arr, begin + 1, end);
// 回复原来字符
char tempChar2 = arr[i];
arr[i] = arr[begin];
arr[begin] = tempChar2;
}
}
}
public static void main(String[] args)
{
String str = "abcd";
char[] arr = str.toCharArray();
// quanPaiLieOfStr(cha);
allSortOfStr(arr, 0, 3);
}
}