[LeetCode] Subsets II [32]

题目

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,

If S = [1,2,2], a solution
is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

原题链接(点我)

解题思路

这个题很subsets这个题一样,不过这里允许给出的集合中含有重复元素,对于这个条件之需要加一个判断条件就可以了,其余代码和Subsets都一样。

代码实现

class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        vector<vector<int> > ret;
        sort(S.begin(), S.end());
        helper(0, S, vector<int>(), ret);
        return ret;
    }

    void helper(int start, const vector<int>& S, vector<int> part, vector<vector<int> >& ret){
        if(start == S.size()) return;
        if(start == 0) ret.push_back(part);
        for(int i=start; i<S.size(); ++i){
            // 这里加了个判断,就可以避免重复组合
            if(i>start && S[i] == S[i-1]) continue;
            part.push_back(S[i]);
            ret.push_back(part);
            helper(i+1, S, part, ret);
            part.pop_back();
        }
    }
};

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30221841
)

[LeetCode] Subsets II [32]

时间: 2024-10-07 08:39:22

[LeetCode] Subsets II [32]的相关文章

[leetcode]Subsets II @ Python

原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicat

LeetCode: Subsets II [091]

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

LeetCode Subsets II

Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution is:

[Leetcode] subsets ii 求数组所有的子集

Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S =[1,2,2], a solution is: [

[LeetCode]Subsets II生成组合序列

class Solution {//生成所有[不重复]的组合.生成组合只要采用递归,由序列从前往后遍历即可.至于去重,根据分析对应的递归树可知,同一个父节点出来的两个分支不能一样(即不能与前一个元素一样,且前一个元素要与之在同层). public: int *b,n; vector<int>a; vector<vector<int> >ans; void dfs(int id,int len){ if(len>0){ vector<int>v(b,b+

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]

【leetcode】Subsets II

Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,2], a so

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2