78. Subsets

Posted

tags:

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

鏍囩锛?/p>

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

class Solution {
public:
    void dfs(vector<vector<int>>&u,int x,vector<int>& nums,vector<int>&v,int cnt,int n){
        if(cnt==n){
            //n++;
            u.push_back(v);
            return ;
        }
        for(int i=x;i<nums.size();i++){
            v.push_back(nums[i]);
            dfs(u,i+1,nums,v,cnt+1,n);
            v.pop_back();
        }    
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>>u; 
        vector<int>v;
        sort(nums.begin(),nums.end());
        for(int i=0;i<=nums.size();i++){
            dfs(u,0,nums,v,0,i);
        }
        return u;
    }
};

 

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

78. Subsets

78. Subsets

78. Subsets

78. Subsets

刷题78. Subsets

Leetcode 78. Subsets