33. Subsets FROM Leetcode

题目

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

频度: 4

解题之法

class Solution {
public:
    vector> subsets(vector& nums) {
        sort(nums.begin(), nums.end());
        vector> subs;
        vector sub;  
        genSubsets(nums, 0, sub, subs);
        return subs; 
    }
    void genSubsets(vector& nums, int start, vector& sub, vector>& subs) {
        subs.push_back(sub);
        for (int i = start; i < nums.size(); i++) {
            sub.push_back(nums[i]);
            genSubsets(nums, i + 1, sub, subs);
            sub.pop_back();
        }
    }
};

你可能感兴趣的:(33. Subsets FROM Leetcode)