码迷,mamicode.com
首页 > 编程语言 > 详细

java递归实现全排列

时间:2015-01-04 17:08:51      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test{
 private static String[] s = new String[]{"1","2","3","4"};
 private static int l = s.length;
 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){
   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递归实现全排列

标签:

原文地址:http://blog.csdn.net/zkn_cs_dn_2013/article/details/42394197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!