Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- vector<vector<int>>ans;
- void fun(int i, int n , int target , vector<int>&a , vector<int>tp)
- {
- if(target < 0) return;
- if(target == 0)
- {
- ans.push_back(tp);
- return ;
- }
- if(i == n-1)
- {
- if(target % a[i] == 0)
- {
- int times = target/a[i];
- while(times>0)
- {
- tp.push_back(a[i]); times--;
- }
- ans.push_back(tp);
- }
- return ;
- }
- tp.push_back(a[i]); fun(i,n,target - a[i],a,tp);
- tp.pop_back();
- fun(i+1,n,target,a,tp);
- }
- public:
- vector<vector<int>> combinationSum(vector<int>& a, int target) {
- int n = a.size();
- vector<int>tp;
- fun(0,n,target,a,tp);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement