90. Subsets II
Given a collection of integers that might contain duplicates,nums, return all possible subsets.
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],
[]
]
思路: 超必杀
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
Arrays.sort(nums);
traceback(list, temp, nums, 0);
return list;
}
public void traceback(List<List<Integer>> list, List<Integer> temp, int[] nums, int start){
list.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]);
traceback(list,temp,nums,i+1);
temp.remove(temp.size()-1);
}
}
}