题解 | #字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
群排列算法加一个集合查重
import java.util.*;
public class Solution {
ArrayList<String> we = new ArrayList<>();
Set<String> you = new HashSet<>();
public ArrayList<String> Permutation(String str) {
String[] list = str.split("");
quanpailie(list, 0);
return we;
}
public void quanpailie(String[] list,int index){
String sp;
if(index == list.length){
String t ="";
for(String i : list){
t=t+i;}
if(you.add(t)){
we.add(t);
}
}else{
for(int i =0;i<list.length;i++){
sp = list[i];
list[i] = list[index];
list[index] = sp;
quanpailie( list, index+1);
sp = list[i];
list[i] = list[index];
list[index] = sp;
}
}
}
}