Leetcode 39. Combination Sum

題目

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:

[
  [7],
  [2, 2, 3]
]

思路

從第一個往前遞歸計算

測試用例

[] 3
[2] 3
[1,2,3,4,5] 8
[2] 0

代碼

package leetcodeArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Leetcode39CombinationSum {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> cur = new LinkedList<Integer>();;
        combination(candidates, target, result, cur, 0);
        return result;
    }

    private void combination(int[] candidate, int target, List<List<Integer>> result, List<Integer> cur, int start){
        if(target == 0){
            result.add(new LinkedList<Integer>(cur));
            return;
        }

        for(int i = start; i < candidate.length && candidate[i] <= target;i++){
            cur.add(candidate[i]);
            combination(candidate, target - candidate[i], result, cur, i);
            cur.remove(cur.size() - 1);
        }

    }
}

結果

這裏寫圖片描述

他山之玉

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> result = new ArrayList<>();
        dfs(result, new LinkedList<Integer>(), candidates, target);
        return result;
    }
    private void dfs(List<List<Integer>> result, LinkedList<Integer> list, int[] arr, int target) {
        if (target == 0) {
            result.add(new LinkedList<Integer>(list));
            return;
        }
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] <= target) {
                list.addFirst(arr[i]);
                dfs(result, list, Arrays.copyOfRange(arr, 0, i + 1), target - arr[i]);
                list.removeFirst();
            }
        }
    }
}
class Solution {
public:
    std::vector<std::vector<int> > combinationSum(std::vector<int> &candidates, int target) {
        std::sort(candidates.begin(), candidates.end());
        std::vector<std::vector<int> > res;
        std::vector<int> combination;
        combinationSum(candidates, target, res, combination, 0);
        return res;
    }
private:
    void combinationSum(std::vector<int> &candidates, int target, std::vector<std::vector<int> > &res, std::vector<int> &combination, int begin) {
        if (!target) {
            res.push_back(combination);
            return;
        }
        for (int i = begin; i != candidates.size() && target >= candidates[i]; ++i) {
            combination.push_back(candidates[i]);
            combinationSum(candidates, target - candidates[i], res, combination, i);
            combination.pop_back();
        }
    }
};
def combinationSum(self, candidates, target):
    res = []
    candidates.sort()
    self.dfs(candidates, target, 0, [], res)
    return res

def dfs(self, nums, target, index, path, res):
    if target < 0:
        return  # backtracking
    if target == 0:
        res.append(path)
        return 
    for i in xrange(index, len(nums)):
        self.dfs(nums, target-nums[i], i, path+[nums[i]], res)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章