[编程题]求数组的所有子集（java实现）

[编程题]求数组的所有子集（java实现） 题目描述

{1,8,5,4,6,7}

[]
[1]
[1, 4]
[1, 4, 5]
[1, 4, 5, 6]
[1, 4, 5, 6, 7]
[1, 4, 5, 6, 7, 8]
[1, 4, 5, 6, 8]
[1, 4, 5, 7]
[1, 4, 5, 7, 8]
[1, 4, 5, 8]
[1, 4, 6]
[1, 4, 6, 7]
[1, 4, 6, 7, 8]
[1, 4, 6, 8]
[1, 4, 7]
[1, 4, 7, 8]
[1, 4, 8]
[1, 5]
[1, 5, 6]
[1, 5, 6, 7]
[1, 5, 6, 7, 8]
[1, 5, 6, 8]
[1, 5, 7]
[1, 5, 7, 8]
[1, 5, 8]
[1, 6]
[1, 6, 7]
[1, 6, 7, 8]
[1, 6, 8]
[1, 7]
[1, 7, 8]
[1, 8]
[4]
[4, 5]
[4, 5, 6]
[4, 5, 6, 7]
[4, 5, 6, 7, 8]
[4, 5, 6, 8]
[4, 5, 7]
[4, 5, 7, 8]
[4, 5, 8]
[4, 6]
[4, 6, 7]
[4, 6, 7, 8]
[4, 6, 8]
[4, 7]
[4, 7, 8]
[4, 8]
[5]
[5, 6]
[5, 6, 7]
[5, 6, 7, 8]
[5, 6, 8]
[5, 7]
[5, 7, 8]
[5, 8]
[6]
[6, 7]
[6, 7, 8]
[6, 8]
[7]
[7, 8]
[8]

``````import java.util.ArrayList;
import java.util.Arrays;
public class Group {
public static void main(String[] args){
int[] arr={1,8,5,4,6,7};
ArrayListIntegerlist=new ArrayList();
ArrayListArrayListIntegerprintlist=new ArrayList();
Arrays.sort(arr);
group(arr,list,printlist,0);
for(ArrayListIntegertem:printlist){
System.out.println(Arrays.toString(tem.toArray()));
}
}
static ArrayListArrayListIntegergroup(int[]arr,ArrayListIntegerlist,ArrayListArrayListIntegerprintlist,int index){
if(list.size()==arr.length)
return printlist;
for(int i=index;iarr.length;i++){
group(arr,list,printlist,i+1);
list.remove(list.size()-1);
}
return printlist;
}
}
``````