62,623
社区成员
发帖
与我相关
我的任务
分享
public class test{
String[] list;
StringBuffer sb = new StringBuffer();
int start;
test(String in){
list = in.split(",");
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?"\n":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
public static void main(String args[]){
new test("1,2,3,4,5,6");
}
}
public class test{
String[] list;
StringBuffer sb = new StringBuffer();
int start;
test(String in){
list = in.split(",");
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?"\n":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
public static void main(String args[]){
new test("1,2,3,4,5,6");
}
}
/**
* @param num how many characters in one result
* @param array range of the alphabets
* @return all sequences
*/
public static String[] getAllSequence(int num, String[] array) {
if (num == 1) {
return array;
}
else {
String[] currentArray = getAllSequence(num - 1, array);
String[] newArray = new String[array.length * currentArray.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < currentArray.length; j++) {
newArray[i * currentArray.length + j] = array[i] + currentArray[j];
}
}
return newArray;
}
}