2ms JAVA Solution


  • 0
    Y
    public class Solution {
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            res.add(new ArrayList<Integer>());
            getSets(res, nums, 0, new ArrayList<Integer>());
            return res;
        }
        
        public void getSets(List<List<Integer>> res, int[] nums, int cur, List<Integer> list){
            if(cur == nums.length)
                return;
            for(int i=cur; i<nums.length; i++){
                list.add(nums[i]);
                res.add(new ArrayList<Integer>(list));
                getSets(res, nums, i + 1, list);
                list.remove(list.size() - 1);
                
                while(i + 1 < nums.length && nums[i] == nums[i + 1]) i++;
            }
            
        }
    }

Log in to reply
 

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