1ms Java version using bit manipulation


  • 0
    O
    public class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            
            for (int i = 0; i < (1 << nums.length); ++i) {
                int n = i;
                List<Integer> sublist = new ArrayList<>();
                while (n != 0) {
                    int t = n & -n;
                    sublist.add(nums[Integer.bitCount(t-1)]);
                    n ^= t;
                }
                result.add(sublist);
                
            }
            
            return result;
        }
    }
    

Log in to reply
 

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