输入一个字符数组,打印出该字符数组中字符的所有不重复的排列。
import java.util.ArrayList;
import java.util.List;
public class Main {
static List<String> lstchs = new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] chs = {'F','G','J','J','H','K','S','L'};
lstchs.clear();
Permutation(chs);
for(int i = 0; i<lstchs.size();i++) {
System.out.println(lstchs.get(i).toString());
}
}
public static void Permutation(char[] chs) {
if(chs.length == 0)
return ;
Permutation(chs,0,0,chs.length);
}
public static void Permutation(char[] chs,int pBegin,int pstr,int len) {
if(pBegin == len) {
String s = "";
int i = 0;
while(i<len) {
s += chs[i];
i += 1;
}
if(lstchs.contains(s) == false)
lstchs.add(s);
}
else {
for(int index=pBegin;index<len;index++) {
char temp = chs[index];
chs[index] = chs[pBegin];
chs[pBegin] = temp;
Permutation(chs,pBegin+1,pstr,len);
temp = chs[index];
chs[index] = chs[pBegin];
chs[pBegin] = temp;
}
}
}
}