标签:int 注意 重复 out tco blog 多次 begin 需要
类似 二叉树遍历到根节点path,也类似深度优先搜索
但是有两点要注意的:
class Solution { void comSum(vector<int> &candidates,vector<vector<int>> &result,int s, vector<int> temp, int target) { if(target==0) { result.push_back(temp); return; } for(int i=s;i<candidates.size();i++) { if(candidates[i]<=target) { temp.push_back(candidates[i]); cout<<candidates[i]<<endl; cout<<target<<endl; // int t=temp.size(); // temp[t]=candidates[i]; comSum(candidates, result,s, temp,target-candidates[i]); temp.pop_back(); } if(candidates[i]>target) return; s=s+1; } } public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int>> result; vector<int> temp; comSum(candidates,result,0, temp, target); return result; } };
标签:int 注意 重复 out tco blog 多次 begin 需要
原文地址:http://www.cnblogs.com/fanhaha/p/7326446.html