My java code using bit


  • 0
    C
     public List<List<Integer>> subsets(int[] nums) {
            long a = 0;  // every bit for whether using the i‘th nums.
            int bit = nums.length;
            int curB = 0;
            List<List<Integer>> temp = new ArrayList<>();
    
            while (curB <= bit) {
                List<Integer> item = new ArrayList<>();
                for (int i = 0; i < curB; i++) {
                    if (((a >> i) & 1) == 1) {
                        item.add(nums[i]);
                    }
                }
                temp.add(item);
    
                curB += (a & (a + 1)) == 0 ? 1 : 0;
                a++;
            }
    
            return temp;
        }
    

Log in to reply
 

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