Leetcode 39. Combination Sum

题目链接:
https://leetcode.cn/problems/combination-sum/## 方法一 回溯

1 方法思想

2 代码实现

class Solution {
List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTracking(candidates, 0, 0, target);
        return result;
    }
    
    public void backTracking(int[] candidates, int start, int sum, int target) {
        
        if (sum>target) return;
        if (sum == target){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;
            path.add(candidates[i]);
            backTracking(candidates, i, sum + candidates[i], target);
            path.removeLast();
        }
    }
}

3 复杂度分析

时间复杂度:
空间复杂度:

4 涉及到知识点

5 总结

你可能感兴趣的:(算法打卡,leetcode,算法,职场和发展)