字符串的排列
与重复数字写法基本相同。
public ArrayList<String> Permutation(String str) {
ArrayList<String> list=new ArrayList<String>();
if(str==null||str.length()==0) return list;
char[] strnum=str.toCharArray();
boolean[] vis=new boolean[str.length()];
Arrays.fill(vis,false);
StringBuilder sr=new StringBuilder();
recure(list,strnum,vis,sr);
return list;
}
public void recure(ArrayList<String> list,char[] chars,boolean[] vis,StringBuilder sr){
if(sr.length()==chars.length) list.add(new String(sr));
for (int i=0;i<chars.length;i++){
if(vis[i]) continue;
if(i>0&&chars[i]==chars[i-1]&&!vis[i-1]) continue;
vis[i]=true;
sr.append(chars[i]);
recure(list,chars,vis,sr);
vis[i]=false;
sr.deleteCharAt(sr.length()-1);
}
}