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.

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

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

思路:
将递归的输入为当前的结果集,当前的数组,和遍历的起始下标。在递归中将起始下标后的值依次加入当前结果集,并将结果集加入结果集数组中。如果遇到重复的数字,则继续遍历下一个数字,直至遍历结束。

class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> fnl = new ArrayList<List<Integer>>();
Arrays.sort(nums);
helper(fnl, new ArrayList<Integer>(), nums, 0);
return fnl;
}

public void helper(List<List<Integer>> fnl, List<Integer> temp, int[] nums, int start){
fnl.add(new ArrayList<Integer>(temp));
for(int i = start; i < nums.length; i++){
if(i > start && nums[i] == nums[i-1])
continue;
temp.add(nums[i]);
helper(fnl, temp, nums, i+1);
temp.remove(temp.size()-1);
}
}
}