题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
import java.util.*;
class Solution {
StringBuffer sf = new StringBuffer();
Set<String> set = new HashSet<>();
public ArrayList<String> Permutation(String str) {
ArrayList<String> alist = new ArrayList<>();
Permutate(str.toCharArray());
for(String s:set){
alist.add(s);
}
return alist;
}
public void Permutate(char[] c){
if(c.length<=1){
sf.append(c[0]);
set.add(sf.toString());
sf.delete(sf.length()-1,sf.length());
return;
}
for(int i=0;i<c.length;i++){
sf.append(c[i]);
char num[] = new char[c.length-1];
int index = 0;
for(int j=0;j<c.length;j++){
if(j==i)continue;
num[index++] = c[j];
}
Permutate(num);
sf.delete(sf.length()-1,sf.length());
}
}
}