Skip to content

Instantly share code, notes, and snippets.

@seveniu
Last active December 24, 2015 21:49
Show Gist options
  • Save seveniu/6868488 to your computer and use it in GitHub Desktop.
Save seveniu/6868488 to your computer and use it in GitHub Desktop.
package com.seveniu;
import java.util.ArrayList;
import java.util.List;
public class Permutation {
private int[] array; //传入数组
private int num; //从数组中选出n个数
private int[] result = new int[num]; //将选出的n个数存入数组
private int count = 0;
private List<int[]> resultList = new ArrayList<int[]>();
public Permutation(int[] array, int num) {
this.array = array;
this.num = num;
this.result = new int[num];
}
public List<int[]> getResult() { //5选3 从sum个数中选n个数
return make(array.length,num);
}
public List<int[]> make(int sum , int n) { //5选3 从sum个数中选n个数
if (n > 0) { //用来判断递归次数
for (int i = array.length - sum; i < array.length; i++) { //从length-sum开始循环
result[this.num - n] = array[i]; //将结果存到数组中
if (n == 1) { //输出存放结果的数组
for (int number : result) {
System.out.print(number + " ");
}
System.out.println(";");
count++; //统计排列总和
resultList.add(result);
}
sum--; //每循环一次,总数减一
make(sum, n - 1); //开始下一层递归
}
}
return resultList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment