子集
逆序
1.位向量法
ArrayList> res=new ArrayList >(); public void helper(int[] A,int cur,boolean[] B){ if(cur<0){ ArrayList arr=new ArrayList (); for(int i=A.length-1;i>=0;i--){ if(B[i]){ arr.add(A[i]); } } if(!arr.isEmpty()){ res.add(arr); } return; } B[cur]=true; helper(A,cur-1,B); B[cur]=false; helper(A,cur-1,B); } public ArrayList > getSubsets(int[] A, int n) { helper(A,n-1,new boolean[A.length]); return res; }
2.二进制法
public ArrayList> getSubsets(int[] A, int n) { ArrayList > res=new ArrayList >(); for(int i=(1< 0;i--){ ArrayList arr=new ArrayList (); for(int j=i,index=0;j>0;j=j>>1,index++){ if((j&1)==1){ arr.add(0,A[index]); } } res.add(arr); } return res; }
3.增量构造法