Java 10 lines 2 ms solution


  • 0
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> list = new ArrayList<>();
            list.add(new ArrayList<>());
            for (int i = 0, begin = 0, size = list.size(); i < nums.length; i++, begin = size, size = list.size()) {
                for (int j = i > 0 && nums[i] == nums[i - 1] ? begin : 0; j < size; j++) {
                    List<Integer> l = new ArrayList<>(list.get(j));
                    l.add(nums[i]);
                    list.add(l);
                }   
            }   
            return list;
        } 
    

Log in to reply
 

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