java组合总和(力扣Leetcode39)

组合总和

力扣原题链接

问题描述

给定一个无重复元素的整数数组 candidates 和一个目标整数 target,找出 candidates 中可以使数字和为目标数 target 的所有不同组合,并以列表形式返回。你可以按任意顺序返回这些组合。

示例

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。7 也是一个候选, 7 = 7 。仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

解题思路

这是一个典型的回溯算法问题。我们需要在给定的数字数组中寻找所有可能的组合,使其和等于目标数 target

代码思路

  1. 初始化结果列表: 创建一个空的列表 result,用于存储最终的组合结果。
  2. 回溯搜索: 定义一个回溯函数 backtrack,其参数包括当前处理的索引 start、当前的目标值 target、当前的组合路径 path
  3. 结束条件: 如果当前目标值等于 0,说明找到了一个有效组合,将其加入结果列表,并返回。
  4. 选择列表: 候选数字数组中的所有数字。
  5. 遍历选择: 从当前处理索引 start 开始遍历候选数字数组。
  6. 做出选择: 将当前数字加入路径,并更新目标值为 target - candidates[i]
  7. 递归进入下一层: 递归调用回溯函数,传入新的索引 i 和更新后的目标值。
  8. 撤销选择: 回溯到上一层时,将当前选择的数字从路径中删除。

Java解题

写法一

直接用target来减,return判断条件为target == 0

import java.util.*;

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

    private void backtrack(int[] candidates, int target, int start, List<Integer> path, List<List<Integer>> result) {
        // 结束条件:如果目标值为 0,说明找到了一个有效组合,将其加入结果列表
        if (target == 0) {
            result.add(new ArrayList<>(path));
            return;
        }
        // 从当前处理索引开始遍历候选数字数组
        for (int i = start; i < candidates.length; i++) {
            // 做出选择:将当前数字加入路径,并更新目标值
            if (target - candidates[i] >= 0) {
                path.add(candidates[i]);
                // 递归进入下一层,传入新的索引和更新后的目标值
                backtrack(candidates, target - candidates[i], i, path, result);
                // 撤销选择:回溯到上一层时,将当前选择的数字从路径中删除
                path.remove(path.size() - 1);
            }
        }
    }
}
写法2

加入一个sum来收集当前path里的总和,return判断条件为sum = target

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> path = new ArrayList<>();
        backstrack(candidates,target,0,0,path);
        return res;

    }
    public void backstrack(int[] candidates,int target,int index,int sum,List<Integer> path){
        if(sum == target){
            res.add(new ArrayList<>(path));
            return;
        }
        for( int i = index;i<candidates.length;i++){
            if(sum>target) return;
            if(candidates[i]>target) continue;
            path.add(candidates[i]);
            backstrack(candidates,target,i,sum+candidates[i],path);
            path.remove(path.size()-1);
        }
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BenChuat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值