C++ easy understand method


  • 0
    B
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> com;
        vector<int> path;
        helper(com,candidates,path,target,0);
        return com;
    }
    void helper(vector<vector<int>>& com,vector<int>& candidates,vector<int>& path,int target,int index)
    {
        if(target == 0)
        {
            com.push_back(path);
            return;
        }
        for(int i = index;i<candidates.size();i++)
        {
            if(candidates[i] > target)
                continue;
            path.push_back(candidates[i]);
            helper(com,candidates,path,target-candidates[i],i);
            path.pop_back();
        }
        
    }

Log in to reply
 

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