My Java solution using bit manipulation, and it takes 284ms


  • 0
    R
        public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        
        int subsetNum = (int)Math.pow(2, nums.length);
        Arrays.sort(nums);
        
        for(int i = 0; i < subsetNum; i++){
        	result.add(new ArrayList<Integer>());
        }
        
        for(int i = 0; i < nums.length; i++){
            for(int j = 1; j < subsetNum; j++){
                List<Integer> temp = result.get(j);
                
                if(((j >> i) & 1) == 1){
                    temp.add(nums[i]);
                }
            }
        }
        
        return result;
    }

Log in to reply
 

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