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], [] ]
public class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<Integer> item = new ArrayList<Integer>();
res.add(item);
sub(nums, 0, item);
return res;
}
private void sub(int[] nums, int index, List<Integer> input) {
for (int i = index; i < nums.length; i++) {
List<Integer> item = new ArrayList<Integer>(input);
item.add(nums[i]);
if (res.contains(item) == false) {
res.add(item);
sub(nums, i + 1, item);
}
}
}
}