分析以下需求,并用代码实现:
(1)打印一个字符串的全字符组合情况,原始字符
串中没有重复字符
(2)例如:原始字符串是"def", 打印得到下列所有组合情况:
"d" "e" "f" "de" "df" "ed" "ef" "fd"
"fe" "def" "dfe" "edf" "efd" "fde" "fed"*/
/*Scanner scanner= new Scanner(System.in);
System.out.println("请输入一个字符串(没有重复字符)");
String line=scanner.nextLine();
char[] cr=line.toCharArray();
TreeSet<String> ts=new TreeSet<String>(new Comparator<String>() {
public int compare(String o1, String o2) {
int num=o1.length()-o2.length();
return num==0?o1.compareTo(o2):num;
};
});
for (int i = 0; i < cr.length; i++) {
ts.add(""+cr[i]);
for (int j = 0; j < cr.length; j++) {
if(cr[i]==cr[j]){
continue;
}
ts.add(""+cr[i]+cr[j]);
for (int k = 0; k < cr.length; k++) {
if(cr[i]==cr[j]||cr[i]==cr[k]||cr[j]==cr[k]){
continue;
}
ts.add(""+cr[i]+cr[j]+cr[k]);
}
}
}
System.out.println(ts);*/
|
|