題目:Given a collection of integers that might contain duplicates,nums, return all possible subsets.
Note:
For example,
Ifnums=[1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]解題思路:這題與第78題相似,不同之處在於這題給定的數組中重復的數字,參照78題思路,再利用HashSet就可以過濾掉重復的list,具體實現見下面代碼:
public class Solution { public List> subsetsWithDup(int[] nums) { List
> result=new ArrayList
>(); HashSet
> temp=new HashSet
>(); //先對數組進行排序 Arrays.sort(nums); int n=nums.length; //1<
list=new ArrayList (); for(int j=0;j list:temp) { result.add(list); } return result; } }