Leetcode:subsets

public class Solution {
    public List<List<Integer>> subsets(int[] S) {
           List<List<Integer>> result=new ArrayList<List<Integer>>();
           Arrays.sort(S);
           int N=S.length;
           int Max=1<<N; //2^N
           for(int i=0;i<Max;i++){
               ArrayList <Integer> subset=new ArrayList<Integer>(); 
               int j=i;
               int idx=0;
               while(j>0){
                   if((j&1)!=0){
                      subset.add(S[idx]) ;
                   }
                   j=j>>1;
                 idx++;
               }
               result.add(subset);
              
           }
           return result;
        }
}


你可能感兴趣的:(java,public)