Java字符串全排列问题
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;
}
}
}
}
这几段代码怎么理解啊
TheLast1996
浏览 1258回答 1
1回答
-
zeng_建军
话说代码贴成这样连看的心情都没了,我还是先帮忙把代码贴好再说吧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;
}
}
}
}
打开App,查看更多内容