数组中找几个数和为sum

 

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<int> ivec;
        vector<vector<int> > res;
        if(target<=0||candidates.size()==0){
            return res;
        }
        sort(candidates.begin(),candidates.end());
        dfs(candidates,0,target,ivec,res);
        return res;
    }
    void dfs(vector<int> &cond,int k,int target,vector<int> temp,vector<vector<int> > &res){
        if(target==0){
            res.push_back(temp);
            return ;
        }else if(target<0){
            return ;
        }
        for(int i=k;i<cond.size();++i){  //不能再往回找了  
            temp.push_back(cond[i]);
            dfs(cond,i,target-cond[i],temp,res);
            temp.pop_back();
        }
    }
};


 

展开阅读全文

没有更多推荐了,返回首页