In this Leetcode Combination Sum problem solution we have given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Leetcode Combination Sum problem solution


Problem solution in Python.

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        candidates.sort()
        res = []
        self.helper(0, candidates, target, [], 0, res)
        return res
        
    def helper(self, i, candidates, target, cur, temp, res):
        for j in range(i, len(candidates)):
            if temp + candidates[j] == target:
                res.append(cur + [candidates[j]])
                return 
            elif temp + candidates[j] < target:
                self.helper(j, candidates, target, cur + [candidates[j]], temp + candidates[j], res)
            else:
                return



Problem solution in Java.

class Solution {
    
    public List<List<Integer>> combinationSum(int[] candidates, int target)
    {
        List<List<Integer>> scombo = new ArrayList<>();
        tryCombo(candidates, target, 0, new ArrayList<>(), scombo);
        return scombo;
    }

    public void tryCombo(int[] candidates, int target, int idxCandidate, List<Integer> currentCombo, List<List<Integer>> combos)
    {
        if (target == 0)
        {
            combos.add(currentCombo);
            return;
        }
        if (target < 0)
        {
            return;
        }

        for (int i = idxCandidate; i < candidates.length; i++)
        {
            int cur = candidates[i];
            if (target - cur < 0)
            {
                continue;
            }

            List<Integer> list = new ArrayList<>(currentCombo);
            list.add(cur);

            tryCombo(candidates, target - cur, i, list, combos);
        }
    }
}


Problem solution in C++.

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> r;
        combin(res,r,candidates,target,0);
        return res;
    }
    void combin(vector<vector<int>> &res, vector<int> r, vector<int>& c, int t, int s){
        if(s == t){
            cout<<s<<endl;
            sort(r.begin(),r.end());
            if(res.size()<=0){
                res.push_back(r);
            }else{
                bool flag =false;
                for(int i=0; i < res.size(); i++){
                    if(r == res[i]){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    res.push_back(r);
                }
            }
            
        }else if(s < t){
            for(int i = 0; i <c.size(); i++ ){
                int sum = s+ c[i];
                r.push_back(c[i]);
                combin(res, r,c,t,sum);
                r.pop_back();
            }
        }
    }
};


Problem solution in C.

#define SIZE 125
int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
    int** ret=(int**)malloc(SIZE*sizeof(int*));
    int count=0;
    int tamp_returnSize=0;
    int** tamp_columnSizes=(int**)malloc(sizeof(int*));
    columnSizes[0]=(int*)malloc(SIZE*sizeof(int));
    if(candidatesSize<1||target<0){
        *returnSize=0;
        free(ret);
        free(tamp_columnSizes);
        return NULL;
    }
    for(int i=0;i<candidatesSize;i++){
        if(target-candidates[i]==0){
            ret[count]=(int*)malloc(sizeof(int));
            columnSizes[0][count]=1;
            ret[count][0]=candidates[i];
            count++;
            continue;
        }
        int** tamp=combinationSum(&candidates[i], candidatesSize-i, target-candidates[i], tamp_columnSizes, &tamp_returnSize);
        for(int k=0;k<tamp_returnSize;k++){
            ret[count]=(int*)malloc((tamp_columnSizes[0][k]+1)*sizeof(int));
            columnSizes[0][count]=tamp_columnSizes[0][k]+1;
            ret[count][0]=candidates[i];
            for(int j=0;j<tamp_columnSizes[0][k];j++){
                ret[count][j+1]=tamp[k][j];
            }
            count++;
        }
        free(tamp);
    }
    free(tamp_columnSizes);
    *returnSize=count;
    return ret;
}