首页 > 代码库 > Java递归实现全排列改进(二)---利用ArrayList实现去重
Java递归实现全排列改进(二)---利用ArrayList实现去重
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test{
private static String[] s = new String[]{"a","b","a"};
private static int l = s.length;
private static List<String> list = new ArrayList<String>();
public static void main(String[] args) {
List<String> source = Arrays.asList(s);
List<String> target = new ArrayList<String>();
sort(source,target);
}
private static void sort(List<String> source, List<String> target) {
if(target.size() == l){
if(!list.contains(target.toString())){
list.add(target.toString());
for(int i=0;i<target.size();i++){
System.out.print(target.get(i));
}
System.out.println();
}
}
for(int i=0;i<source.size();i++){
List<String> targetList = new ArrayList<String>(target);
targetList.add(source.get(i));
List<String> resourceList = new ArrayList<String>(source);
resourceList.remove(i);
sort(resourceList,targetList);
}
}
}
Java递归实现全排列改进(二)---利用ArrayList实现去重