回溯+剪枝(排除重复部分)
字符串的排列
http://www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7
public class Solution {
ArrayList<string> res = new ArrayList<>();
StringBuilder path = new StringBuilder();
boolean[] visited;
public ArrayList<string> Permutation(String str) {
if(str==null || str=="") return res;
char[] strs = str.toCharArray();
Arrays.sort(strs);
visited = new boolean[strs.length];
dfs(strs,0);
return res;
}
private void dfs(char[] strs,int depth){
if(depth==strs.length){
res.add(path.toString());
return;
}
for(int i=0;i<strs.length;i++){
if(i>0&&strs[i]==strs[i-1]&&visited[i-1]==false) continue;
if(!visited[i]){
visited[i]=true;
path.append(strs[i]);
dfs(strs,depth+1);
visited[i]=false;
path.deleteCharAt(path.length()-1);
}
}
}
}</string></string>