Java iterative 4 lines 2ms (bit masks)


  • -1
    M
    public class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            List<Integer> subset = new ArrayList<Integer>();
            for(int i = 0, j = -1; i < 1 << nums.length; i++, res.add(subset), subset = new ArrayList<Integer>(), j = -1) while(++j < nums.length) if(((1 << j) & i) == (1 << j)) subset.add(nums[j]);
            return res;
        }
    }
    

    Decided to do a litttle bit of code golfing in Java :) Solution based on bit masks.


Log in to reply
 

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