美文网首页
33. Subsets FROM Leetcode

33. Subsets FROM Leetcode

作者: 时光杂货店 | 来源:发表于2017-03-17 19:13 被阅读10次

    题目

    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<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<vector<int>> subs;
            vector<int> sub;  
            genSubsets(nums, 0, sub, subs);
            return subs; 
        }
        void genSubsets(vector<int>& nums, int start, vector<int>& sub, vector<vector<int>>& 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

          本文链接:https://www.haomeiwen.com/subject/irhynttx.html