子集

逆序

1.位向量法

  ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
    public void helper(int[] A,int cur,boolean[] B){
        if(cur<0){
            ArrayList<Integer> arr=new ArrayList<Integer>();
            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<ArrayList<Integer>> getSubsets(int[] A, int n) {
        helper(A,n-1,new boolean[A.length]);
        return res;
    }

2.二进制法

public ArrayList<ArrayList<Integer>> getSubsets(int[] A, int n) {
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        for(int i=(1<<n)-1;i>0;i--){
            ArrayList<Integer> arr=new ArrayList<Integer>();
            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.增量构造法

原文地址:https://www.cnblogs.com/wqkant/p/6817785.html