My 17 lines Java


  • 1
    M
    public class Solution {
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> ret = new ArrayList<>();
            Arrays.sort(nums);
            helper(ret, new ArrayList<Integer>(), nums, 0);
            return ret;
        }
        private void helper(List<List<Integer>> ret, List<Integer> list, int[] nums, int start) {
            ret.add(list);
            for (int i=start; i<nums.length; i++) {
                if (i != start && nums[i] == nums[i-1]) continue; // avoid duplicates
                List<Integer> nlist = new ArrayList<Integer>(list);
                nlist.add(nums[i]);
                helper(ret, nlist, nums, i+1);
            }
        }
    }

Log in to reply
 

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