题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
穷举,排列
排列之后,字符串有些是一样的,所有要有重操作
import java.util.ArrayList;
public class Solution {
//穷举
ArrayList<String> res;
String path;
int[] used;
public ArrayList<String> Permutation(String str) {
res = new ArrayList<>();
path = "";
used = new int[str.length()];
if(str.length() == 0) return res;
track(str, path);
return res;
}
public void track(String str, String path){
if(path.length() == str.length()){
if(!res.contains(path)) res.add(path);//去重
return;
}
for(int i = 0; i < str.length(); i++){
if(used[i] == 1) continue;
used[i] = 1;
track(str, path+str.charAt(i));
used[i] = 0;//回溯
}
}
}