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 solution
is:

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

【题意】

给定一个可能包含重复值的整数集合S,返回所有可能的子集合

1. 要求子集中的元素必须非递减排列

2. 返回的结果中不能出现重复的集合

【思路】

Subsets相同,先对集合排序,保证得到的子集非递减,然后递归的求出长度分别为0,1,2,3,4..n的所有组合。多了一步就是注意组合的去重。

【代码】

class Solution {
public:

    void getCombinations(vector<vector<int> >&result, vector<int>&s, vector<int>combination, int index2add, int kth, int k){
        //将s中的第index2add位上的数作为组合中的第kth个数
        combination.push_back(s[index2add]);
        if(kth==k){
            result.push_back(combination);
            return;
        }

        for(int i=index2add+1; i+(k-kth-1)<s.size(); i++){
            if(i!=index2add+1 && s[i]==s[i-1])continue; //去重
            getCombinations(result, s, combination, i, kth+1, k);
        }
    }

    vector<vector<int> > getSubsetK(vector<int>&s, int k){
        // 求长度为k的子集
        vector<vector<int> >result;
        vector<int> combination;
        for(int i=0; i+(k-1)<s.size(); i++){
            if(i!=0 && s[i]==s[i-1])continue;   //排重
            getCombinations(result, s, combination, i, 1, k);
        }
        return result;
    }

    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        vector<vector<int> >result;
        vector<int> emptySet;   //一定有个空集
        result.push_back(emptySet);

        int size=S.size();
        if(size==0)return result;

        sort(S.begin(), S.end());

        for(int k=1; k<=size; k++){
            //分别求出长度为1,2,3,...size的子集
            vector<vector<int> >subsets=getSubsetK(S, k);
            result.insert(result.end(), subsets.begin(), subsets.end());
        }
        return result;
    }
};

LeetCode: Subsets II [091],布布扣,bubuko.com

时间: 2024-07-30 13:48:58

LeetCode: Subsets II [091]的相关文章

[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 [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

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 &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

【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