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], [up ], []]
This problem is the extension of subsets, except that there are repeating numbers in the array.
So every time I meet a duplicate number, continue is good. (Note that flag must be set to have flag, otherwise there will be a duplicate number in the result).
Public classSolution {List<List<Integer>> res =NewArraylist<>(); int[] num; intLen; PublicList<list<integer>> Subsetswithdup (int[] nums) {Len=nums.length; Num=Nums; Arrays.sort (num); Res.add (NewArraylist<>()); Sub (0,NewArraylist<>() ); returnRes; } Public voidSubintStart,list<integer>ans) { intFlag = 0; for(inti = start;i<len;i++){ if(i > 0 && flag = = 1 && num[i] = = Num[i-1]) Continue; Ans.add (Num[i]); Flag= 1; Res.add (NewArraylist<integer>(ans)); Sub (i+1, ans); Ans.remove (Ans.size ()-1); } return ; }}
Leetcode Subsets II-----java