| 1 | class Solution { |
| 2 | public: |
| 3 | vector<vector<int>> ans; |
| 4 | |
| 5 | void helper(vector<int>& candidates, int target,int i,int curSum, vector<int> temp){ |
| 6 | // Bounding Condition |
| 7 | if (curSum>target){ |
| 8 | return; |
| 9 | } |
| 10 | // Base Condition |
| 11 | if (i==candidates.size()){ |
| 12 | if(curSum==target){ |
| 13 | ans.push_back(temp); |
| 14 | } |
| 15 | return; |
| 16 | } |
| 17 | // Inclusion |
| 18 | curSum+=candidates[i]; |
| 19 | temp.push_back(candidates[i]); |
| 20 | helper(candidates,target,i,curSum,temp); |
| 21 | curSum-=candidates[i]; |
| 22 | temp.pop_back(); |
| 23 | |
| 24 | // Exclusion |
| 25 | helper(candidates,target,i+1,curSum,temp); |
| 26 | } |
| 27 | vector<vector<int>> combinationSum(vector<int>& candidates, int target) { |
| 28 | vector<int> temp; |
| 29 | helper(candidates,target,0,0,temp); |
| 30 | return ans; |
| 31 | |
| 32 | } |
| 33 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected