1ms, beats 94.62%, bit manipulation Java


  • 0
    J
       public List<List<Integer>> subsets(int[] arr) {
          if (arr == null ) 
             return new ArrayList<>();
    
          List<List<Integer>> subsets = new ArrayList<>();
          int setSize = (int) Math.pow(2, arr.length);
          for (int i = 0; i < setSize; i++) {
          int index = 0;
          int j = i;
          List<Integer> set = new ArrayList<>();
          while (j != 0) {
            if ((j & 1) == 1) {
            set.add(arr[index]);
           }
           index++;
           j = j >> 1;
        }
       subsets.add(set);
     }
     return subsets;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.