90. Subsets II (Recursion, DP)

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solution is:

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

思路II: 对于重复了n次的字符,可以选择放入0,1,2...n个

class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        vector<vector<int>> result;
        vector<int> pre;
        if(S.size()==0)
            return result;
        sort(S.begin(),S.end());
        result.push_back(pre);
        dfs(S,result,pre,0);
        return result;
    }
    void dfs(vector<int> &S , vector<vector<int>> &result ,vector<int> pre , int depth)
    {
        if(depth == S.size())  return; //teminate condition

        int dupCounter = 0;
        int dupNum = 0;
        while(depth+1 < S.size() && S[depth] == S[depth+1]) //get duplicate times
        {
            depth++;
            dupNum++;
        }
        while(dupCounter++ <= dupNum) //push duplicate elements
        {
            pre.push_back(S[depth]);
            result.push_back(pre);
            dfs(S,result,pre,depth+1);
        }
        dupCounter = 0;
        while(dupCounter++ <= dupNum) //backtracking
        {
            pre.pop_back();
        }
        dfs(S, result,pre, depth+1); //push none, dfs directly
    }
};
时间: 2024-10-10 23:59:44

90. Subsets II (Recursion, DP)的相关文章

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 Problem 90. Subsets II

python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ """ 思路整理:DFS recursion 1)对nums进行排序以避免nums中重复元素

【LeetCode】90.Subsets II

Subsets II Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2]

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], [] ] 题目标签:Arr

leetcode 90 Subsets II ----- java

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], [] ] 这道题就是Sub

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], [] ] 链接:h

leetCode 90.Subsets II(子集II) 解题思路和方法

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a soluti

[leedcode 90] Subsets II

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a solutio

[leetcode]90. Subsets II数组子集(有重)

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. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode