78 [LeetCode] Subsets

Posted WhoDreamsSky

tags:

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

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]





class Solution {
public:
  vector<vector<int>> subsetsWithDup(vector<int> &S) {
        vector<vector<int>> totalset = {{}};
        sort(S.begin(),S.end());
        for(int i=0; i<S.size();){
            int count = 0; // num of elements are the same
            while(count + i<S.size() && S[count+i]==S[i])  count++;
            int previousN = totalset.size();
            for(int k=0; k<previousN; k++){
                vector<int> instance = totalset[k];
                for(int j=0; j<count; j++){
                  
                    instance.push_back(S[i]);
                      totalset.push_back(instance);
                    
                }
            }
            i += count;
        }
        return totalset;
        } 
};

 





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

LeetCode 78: Subsets

Leetcode 78. Subsets

Leetcode 78. Subsets

[leetcode-78-Subsets]

Leetcode 78: Subsets

LeetCode78. Subsets