90. 子集 II

排序!!
同组合三,两种去重方法:

  1. 使用used数组,判断前一数字是否重复
  2. i>startIndex&&nums[i]==nums[i-1]
    1. class Solution {
    2. public:
    3. void backtracking(vector<int>& nums,int startIndex)
    4. {
    5. result.push_back(path);
    6. for(int i=startIndex;i<nums.size();i++)
    7. {
    8. //去重
    9. if(i>startIndex&&nums[i]==nums[i-1])
    10. continue;
    11. path.push_back(nums[i]);
    12. backtracking(nums,i+1);
    13. path.pop_back();
    14. }
    15. }
    16. vector<vector<int>> subsetsWithDup(vector<int>& nums) {
    17. sort(nums.begin(),nums.end());
    18. backtracking(nums,0);
    19. return result;
    20. }
    21. vector<int> path;
    22. vector<vector<int>> result;
    23. };