标签:
感觉自己代码写的有点复杂,不过好歹是通过了,接下来需要慢慢的把代码写简洁点,通用点。
class Solution { private: vector<vector<int>> res; vector<int> temp; public: void combinationSum(vector<int>& candidates, int target,vector<int>::iterator initer,int tempsum) { if(tempsum==target) { if(find(res.begin(),res.end(),temp)==res.end()) res.push_back(temp); return ; } if(initer==candidates.end()||tempsum>target) return ; for(vector<int>::iterator iter=initer;iter!=candidates.end();iter++) { temp.push_back(*iter); combinationSum(candidates,target,iter+1, tempsum+*iter); temp.pop_back(); } } vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<int>::iterator initer=candidates.begin(); combinationSum(candidates,target,initer,0); return res; } };
标签:
原文地址:http://www.cnblogs.com/qiaozhoulin/p/4513941.html