标签:continue bin void cto end col start style ons
1 class Solution 2 { 3 vector<vector<int>> res; 4 int sum = 0; 5 public: 6 void helper(vector<int>& nums, int start,int target, vector<int>& out) 7 { 8 if(sum == target) 9 { 10 res.push_back(out); 11 } 12 13 if(sum > target) return; 14 15 for (int i = start; i < nums.size(); ++i) 16 { 17 //去重 18 if(i != start && nums[i] == nums[i - 1]) continue; 19 20 out.push_back(nums[i]); 21 sum += nums[i]; 22 helper(nums, i + 1,target,out);//每个数字在每个组合中只能使用一次,所以是 i+1 23 24 out.pop_back(); 25 sum -= nums[i]; 26 } 27 } 28 29 vector<vector<int>> combinationSum2(vector<int>& nums, int target) 30 { 31 vector<int> out; 32 sort(nums.begin(),nums.end()); 33 helper(nums,0,target,out); 34 return res; 35 } 36 };
标签:continue bin void cto end col start style ons
原文地址:https://www.cnblogs.com/yuhong1103/p/12514489.html