Subsets II

1. Title

Subsets II

2.   Http address

https://leetcode.com/problems/subsets-ii/

3. The question

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

4. My code (AC)

 1 public static void helper(int [] nums, int start,List<Integer> sub_set, List<List<Integer>> res_set)
 2     {
 3         int len = nums.length;
 4         if ( start > len)
 5             return;
 6         res_set.add(new ArrayList<Integer>(sub_set));
 7         for(int i = start ; i < len; i++)
 8         {
 9             if ( i != start && nums[i] == nums[i-1])
10                 continue;
11             sub_set.add(nums[i]);
12             helper(nums, i + 1, sub_set, res_set);
13             sub_set.remove(sub_set.size() - 1);
14         }
15     }
16     
17     // Accepted
18     public static List<List<Integer>> subsetsWithDup(int[] nums) {
19         
20         Arrays.sort(nums);
21         
22         List<List<Integer>> res_set = new ArrayList<List<Integer>>();
23         helper(nums, 0, new ArrayList<Integer>(), res_set);
24         return res_set;
25     }
原文地址:https://www.cnblogs.com/ordili/p/4928331.html