[leetcode-90-Subsets II]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-90-Subsets II]相关的知识,希望对你有一定的参考价值。

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],
[1,2],
[]
]

思路:

与不含重复元素相比,只是添加了一句代码验证一下。

void getsubset(vector<vector<int>>& subs,vector<int>& sub,vector<int>& nums,int start)
{
  subs.push_back(sub);
  for(int i= start;i<nums.size();i++)
  {
    if(i==start || nums[i] != nums[i-1])
    {
      sub.push_back(nums[i]);
      getsubset(subs,sub,nums,i+1);
      sub.pop_back();
    }
  }
}
vector<vector<int>> subsetsWithDup(vector<int>& nums)
{
  vector<vector<int>> subs;
  vector<int>sub;
  sort(nums.begin(),nums.end());
  getsubset(subs,sub,nums,0);
  return subs;
}

参考:

https://discuss.leetcode.com/topic/13543/accepted-10ms-c-solution-use-backtracking-only-10-lines-easy-understand











以上是关于[leetcode-90-Subsets II]的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 90. Subsets II 子集合 II

Leetcode 90. Subsets II

[leetcode-90-Subsets II]

Leetcode 90: Subsets II

Leetcode 90. Subsets II

LeetCode 90. Subsets II