Given a collection of integers that might contain duplicates, Nums, return all possible subsets.
Note:
- Elements in a subset must is 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], [up ], []]
Given an array, return all possible subcollections, the array has repeating elements, but the subcollections cannot be duplicated.
Problem-solving ideas: This problem I first sort, and then traverse, the current element is not a duplicate of the element will be in the res all the list is removed and the current element is added to the res, is a repeating element of the last addition to the Res list of this element, and then add a repeating element to the Res.
PublicList<list<integer>> Subsetswithdup (int[] nums) {List<List<Integer>> res =NewArraylist<>(); if(Nums = =NULL|| Nums.length = = 0) { returnRes; } arrays.sort (Nums); Res.add (NewArraylist<integer>()); intLastsize = 0, Currsize; for(inti = 0; i < nums.length; i++) { intStart = (i > 0 && nums[i] = = Nums[i-1])? lastsize:0; Currsize=res.size (); for(intj = start; J < Currsize; J + +) {List<Integer> tmp =NewArraylist<>(Res.get (j)); Tmp.add (Nums[i]); Res.add (TMP); } lastsize=currsize; } returnRes; }
Subsets Ii--leetcode