Share my solution ,12ms, c++


  • 0
    Y
        enter code here
       class Solution {
    public:
        vector<vector<int>> res;
        vector<int> path;
    public:
        vector<vector<int>> combinationSum(vector<int>& can, int target) {
            sort(can.begin(),can.end());
            dfs(can,target,0);
            return res;
        }
        void dfs(vector<int>& can,int target,int begin){
            if(target==0){
                res.push_back(path);
                return ;
            }
            for(int i=begin;i<can.size()&&target-can[i]>=0;i++){
                path.push_back(can[i]);
                dfs(can,target-can[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.