LeetCode: 78. Subsets

转载请注明, 原文地址: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.
link: https://leetcode.com/problems/subsets/

dfs递归即可.

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        auto size=nums.size();
        vector<int> temp;
        vector<vector<int>> res;
        res.push_back(temp);
        dfs(0,size,temp,res,nums);
        return res;
    }

    void dfs(int s,size_t size,
             vector<int>& temp,vector<vector<int>>& res,vector<int>& nums){
        for(;s<size;++s){
            temp.push_back(nums[s]);
            res.push_back(temp);
            dfs(s+1,size,temp,res,nums);
            temp.pop_back();
        }
    }
};

转载请注明:

转载自YuLai's Blog,原文地址:LeetCode: 78. Subsets

发表评论

发表评论

*

沙发空缺中,还不快抢~