0
点赞
收藏
分享

微信扫一扫

【Docker】docker入门之dockerfile编写

一只1994 2023-09-04 阅读 114

题目链接:LeetCode-39-组合总和

class Solution {
    List<List<Integer>> res = new ArrayList<>();// 创建两个全局变量
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTracking(candidates, target, 0,0);
        return res;
    }

    public void backTracking(int[] candidates, int target, int index, int curSum){
        if (target == curSum){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            if (curSum+candidates[i] > target){// 这种情况也要考虑到
                continue;
            }
            path.add(candidates[i]);
            backTracking(candidates,target,i,curSum+candidates[i]);
            path.remove(path.size()-1);
        }
    }
}
举报

相关推荐

0 条评论