leetcode || 78、Subsets

problem:

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

Hide Tags

Array Backtracking Bit
Manipulation

题意:给定一个数组,求出其所有的子集,包括空集

thinking:

(1)77题中已经使用DFS求出数组的指定个数为K的所有子集,这里K的取值范围为1~n,n为数组长度

具体参考:http://blog.csdn.net/hustyangju/article/details/44974825

code:

class Solution {
private:
    vector<vector<int> > ret;
    vector<int> tmp;
public:
    vector<vector<int> > subsets(vector<int> &S) {
    ret.clear();
    unsigned int n=S.size();
    if(n==0)
        return ret;
    sort(S.begin(),S.end());
    tmp.clear();
    ret.push_back(tmp);
    for(int k=1;k<=n;k++)
    {
        tmp.resize(k);
        dfs(0,n,S,k,0);
    }
    return ret;
    }
protected:
    void dfs(int dep, int n, vector<int> &S,int k,int start)
    {
       if(dep==k)
       {
           ret.push_back(tmp);
           return;
       }
       for(int i=start;i<n;i++)
       {
           tmp[dep]=S[i];
           dfs(dep+1,n,S,k,i+1);
       }
    }
};

另一种DFS法:

class Solution {
private:
    vector<vector<int> > ret;
public:
    void dfs(int dep, int maxDep, vector<int> &num, vector<int> a, int start)
    {
        ret.push_back(a);

        if (dep == maxDep)
            return;

        for(int i = start; i < num.size(); i++)
        {
            vector<int> b(a);
            b.push_back(num[i]);
            dfs(dep + 1, maxDep, num, b, i + 1);
        }
    }

    vector<vector<int> > subsets(vector<int> &S) {
        sort(S.begin(), S.end());
        ret.clear();
        vector<int> a;
        dfs(0, S.size(), S, a, 0);

        return ret;
    }
};
时间: 2024-10-26 08:22:14

leetcode || 78、Subsets的相关文章

(LeetCode 78)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. 题目要求 :求整数数组的所有子集 注意: 1.子集元素按非降序排列 2.不包含重复的子集 解题思路: 求解这类诸如子集的题目,都可以采用回溯法

leetcode || 90、Subsets II

problem: 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 sol

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset 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

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

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中重复元素

&amp;lt;LeetCode OJ&amp;gt; 78 / 90 Subsets (I / II)

Given a set of distinct integers, 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,3], a solution is: [ [3], [1], [2], [1,2

[LeetCode] 78. Subsets 子集合

Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Python:

LeetCode 78. Subsets 20170606

Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 题目大意:给定一个整数集合,返回其所有子集的集合