首页 > 编程语言 > 详细

Java递归实现全排列改进(一)---利用HashSet实现去重

时间:2015-01-04 17:06:32      阅读:299      评论:0      收藏:0      [点我收藏+]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Test{
 private static String[] s = new String[]{"a","b","a"};
 private static int l = s.length;
 private static Set<String> set = new HashSet<String>();
 public static void main(String[] args) {
  List<String> source = Arrays.asList(s);
  List<String> target = new ArrayList<String>();
  sort(source,target);
  Iterator<String> it = set.iterator();
  while(it.hasNext()){
   System.out.println(it.next());
  }
 }

 private static void sort(List<String> source, List<String> target) {
  if(target.size() == l){
   set.add(target.toString().replaceAll(",", "").replace("[", "").replace("]", "").replace(" ", ""));
  }
  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递归实现全排列改进(一)---利用HashSet实现去重

原文:http://blog.csdn.net/zkn_cs_dn_2013/article/details/42394467

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!