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:
This problem is a more general case of subset subsets, where there are duplicates in a given set, you can use the same method as combination Sum II to eliminate duplicate elements.
Do now find a lot of problems are comprehend by analogy.
Runtime:8ms
classSolution { Public: vector<vector<int>>Subsetswithdup ( vector<int>& Nums) { vector<int>Path vector<vector<int>>Result Result.push_back (path); Sort (Nums.begin (), Nums.end ()); Helper (Nums,0, Path,result);returnResult }voidHelper vector<int>&nums,intPos vector<int>& Path, vector<vector<int>>&result) {if(Pos==nums.size ())return; for(intI=pos;i<nums.size (); i++) {path.push_back (nums[i]); Result.push_back (path); Helper (nums,i+1, Path,result); Path.pop_back (); while(nums[i]==nums[i+1]) i++; } }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode90:subsets II