JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

Code

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums == null || nums.length == 0) return res;
        Arrays.sort(nums);

        helper(res, nums, 0, new ArrayList<>());
        return res;
    }

    private void helper(List<List<Integer>> res, int[] nums, int index, List<Integer> temp){
        res.add(new ArrayList<>(temp));

        for(int i = index; i < nums.length; i++){
            if(i != index && nums[i] == nums[i - 1]) continue;
            temp.add(nums[i]);
            helper(res, nums, i + 1, temp);
            temp.remove(temp.size() - 1);
        }
    }
}