public class allSequence {
public static void main(String args[]){
char[] array = new char[] { 'a', 'b', 'c' };
Permutation(array);
}
public static void Permutation(char[] pstr){
if(pstr.length==0)
return;
Permutation(pstr,0,pstr.length-1);
}
//核心代码
private static void Permutation(char[] pstr, int start, int end) {
// 递归操作,当递归到最后一个字符,就打印出整个字符序列
if(start==end){
for (int j = 0; j < pstr.length; j++) {
System.out.print(pstr[j]);
}
System.out.println();
}else{
/*
* 这步要做的是递归操作,将第一个字符与后面的每一个元素交换,第二个字符也与后面的每一个元素交换
* 这是一个递归操作,目的就是改变原数组的顺序。需要注意的是,在每次修改后都要还原
*/
for(int i=start;i<=end;i++){
char temp=pstr[start];
pstr[start]=pstr[i];
pstr[i]=temp;
Permutation(pstr, start+1, end);
temp=pstr[start];
pstr[start]=pstr[i];
pstr[i]=temp;
}
}
}
}
每天一个算法之字符数组全排列
最新推荐文章于 2023-03-04 14:40:28 发布