首页 > 代码库 > leetcode Combination Sum

leetcode Combination Sum

void dfs(int k,int target,vector<int>& candidates,vector<int>& sol,vector<vector<int> >& res){	if(target==0){		vector<int> temp(sol);		res.push_back(temp);		return;	}else if(target<0){		return;	}else{		for(int i=k;i<candidates.size();i++){            if(candidates[i]>target) break;			sol.push_back(candidates[i]);			dfs(i,target-candidates[i],candidates,sol,res);			sol.pop_back();		}	}}vector<vector<int> > combinationSum(vector<int> &candidates, int target) {    vector<vector<int> > res;	vector<int> sol;    sort(candidates.begin(),candidates.end());	dfs(0,target,candidates,sol,res);	return res;}