78. Subsets

image.png

经典的回溯法

class Solution {
public:
    vector> subsets(vector& nums) {
        vector> result;
        if(nums.empty()) return result;
        vector list;
        dfs(0, result,list,nums);
        return result;
        
    }
private:
    void dfs(int pos, vector>& result,  vector& list, vector& nums){
        result.push_back(list);
        for(int i = pos; i < nums.size(); i++){
            list.push_back(nums[i]);
            dfs(i+1, result, list, nums);
            list.pop_back();
        }
    }
};

你可能感兴趣的:(78. Subsets)