Leetcode:Subsets 子集生成

Subsets

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

解题分析:

每个元素,都有两种选择,选或者不选。

为了保持子集有序,必须首先对原数组sort

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int>> result;
        if (S.size() == 0) return result;
        sort(S.begin(), S.end());   // sort
        vector<int> path;
        dfs(S, path, 0, result);
        return result;
    }

    void dfs(const vector<int>& S, vector<int>&path, int cur, vector<vector<int>>& result) {
        if (cur == S.size()) {
            result.push_back(path);
            return;
        }
        // not choose S[cur]
        dfs(S, path, cur + 1, result);
        // choose S[cur]
        path.push_back(S.at(cur));
        dfs(S, path, cur + 1, result);
        path.pop_back();
    }
};

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:

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

解题分析:

这题有重复元素,但本质上,跟上一题很类似,上一题中元素没有重复,相当于每个元素只能选0或1次,这里扩充到了每个元素可以选0到若干次而已

class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        vector<vector<int>> result;
        if (S.size() == 0) return result;
        sort(S.begin(), S.end());
        vector<int> path;
        dfs(S, path, 0, result);
        return result;
    }

    void dfs(const vector<int>& S, vector<int>& path, int cur, vector<vector<int>>& result) {
        result.push_back(path);
        for (int i = cur; i < S.size(); ++i) {
            if (i != cur && S.at(i) == S.at(i-1)) {
                continue;
            }

            path.push_back(S.at(i));
            dfs(S, path, i + 1, result);
            path.pop_back();
        }
    }
};

Leetcode:Subsets 子集生成

时间: 2024-08-26 22:00:35

Leetcode:Subsets 子集生成的相关文章

[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:Subsets 求数组的所有子集

Given 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,3], [

Leetcode: Subsets &amp; SubsetsII

Subsets Description: Given 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],

[LeetCode] Subsets [31]

题目 Given 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,3]

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 @ 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 [078]

[题目] Given 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 @ Python

原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @param S, a list of integer # @return a list of lists of integer def subsets(self, S): def dfs(depth, start, valuelist): res.append(valuelist) if depth