package projectdesign;
import java.util.ArrayList;
public class test {
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
String[] strs = {"a","b","c"};
perm(strs, 0, strs.length);
for(String s:list){
System.out.println(s);
}
list.clear();
}
private static void perm(String[] strs,int start,int end){
String s = "";
if(start==end){
for(int i=0;i<strs.length;i++){
s+=strs[i];
}
list.add(s);
}else{
for(int i=start;i<end;i++){
String str = strs[start];
strs[start] = strs[i];
strs[i] = str;
perm(strs,start+1,end);
strs[i] = strs[start];
strs[start] = str;
}
}
}
}
这几段代码怎么理解啊
1 回答
zeng_建军
TA贡献25条经验 获得超16个赞
话说代码贴成这样连看的心情都没了,我还是先帮忙把代码贴好再说吧
import java.util.ArrayList; public class test { static ArrayList<String> list = new ArrayList<String>(); public static void main(String[] args) { String[] strs = { "a", "b", "c" }; perm(strs, 0, strs.length); for (String s : list) { System.out.println(s); } list.clear(); } private static void perm(String[] strs, int start, int end) { String s = ""; if (start == end) { for (int i = 0; i < strs.length; i++) { s += strs[i]; } list.add(s); } else { for (int i = start; i < end; i++) { String str = strs[start]; strs[start] = strs[i]; strs[i] = str; perm(strs, start + 1, end); strs[i] = strs[start]; strs[start] = str; } } } }
添加回答
举报
0/150
提交
取消