Beats 87.25% of cppsubmissions with simple dfs


  • -1
    C

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


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.