LeetCode39:组合总和

原题地址:. - 力扣(LeetCode)

题目描述

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

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 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
输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

解题思路

从给定的候选数字数组 candidates 中找出所有和为 target 的组合。这个问题可以通过深度优先搜索(DFS)算法来解决。代码中定义了两个方法:

  1. combinationSum:主函数,用于初始化结果列表 ans 和临时组合列表 combine,并调用 dfs 方法开始深度优先搜索。
  2. dfs:深度优先搜索函数,用于遍历候选数字数组并构建所有可能的组合。

dfs 方法中,我们有两个选择:

  • 跳过当前数字 :直接递归调用 dfs 方法,索引加一,尝试下一个数字。
  • 选择当前数字 :如果当前目标 target 大于等于候选数字,则将该数字添加到 combine 列表中,并递归调用 dfs 方法,目标减去该数字,索引不变,尝试构建下一个数字的组合。递归结束后,需要从 combine 中移除最后一个数字,以回溯到上一个状态。

源码实现

java 复制代码
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // 初始化结果列表和临时组合列表
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> combine = new ArrayList<>();
        // 从索引0开始深度优先搜索
        dfs(candidates, target, ans, combine, 0);
        return ans;
    }

    public void dfs(int[] candidates, int target, List<List<Integer>> ans, List<Integer> combine, int idx) {
        // 递归终止条件:如果索引等于候选数字数组的长度,返回
        if (idx == candidates.length) {
            return;
        }
        // 如果目标和为0,说明找到了一个有效的组合,添加到结果列表中
        if (target == 0) {
            ans.add(new ArrayList<>(combine));
            return;
        }
        // 直接跳过当前数字,尝试下一个数字
        dfs(candidates, target, ans, combine, idx + 1);
        // 如果当前目标减去当前数字大于等于0,说明可以选择当前数字
        if (target - candidates[idx] >= 0) {
            // 选择当前数字,添加到临时组合列表中
            combine.add(candidates[idx]);
            // 递归调用,目标更新为新的和,索引不变
            dfs(candidates, target - candidates[idx], ans, combine, idx);
            // 回溯,移除临时组合列表中的最后一个数字
            combine.remove(combine.size() - 1);
        }
    }
}

复杂度分析

  • 时间复杂度 :最坏情况下,DFS 会尝试所有可能的组合,时间复杂度为 O(2^n),其中 n 是候选数字数组 candidates 的长度。

  • 空间复杂度:空间复杂度主要取决于递归栈的深度和临时组合列表的大小。最坏情况下,递归栈的深度和临时组合列表的大小都不超过 n,所以空间复杂度为 O(n)

相关推荐
小白学大数据17 小时前
Python爬虫实现无限滚动页面的自动点击与内容抓取
开发语言·爬虫·python·pandas
Andy Dennis17 小时前
一文漫谈设计模式之创建型模式(一)
java·开发语言·设计模式
兩尛17 小时前
c++遍历容器(vector、list、set、map
开发语言·c++
belldeep17 小时前
Java:Tomcat 9 和 mermaid.min.js 10.9 上传.csv文件实现 Markdown 中 Mermaid 图表的渲染
java·tomcat·mermaid·去除flexmark
£漫步 云端彡17 小时前
Golang学习历程【第十三篇 并发入门:goroutine + channel 基础】
开发语言·学习·golang
偷吃的耗子17 小时前
[CNN算法理解]:二、卷积层(从生活实例到技术细节)
算法·cnn·生活
2301_7903009617 小时前
C++与Docker集成开发
开发语言·c++·算法
TracyCoder12317 小时前
LeetCode Hot100(22/100)——141. 环形链表
算法·leetcode·链表
AutumnorLiuu17 小时前
C++并发编程学习(二)—— 线程所有权和管控
java·c++·学习
Demon_Hao17 小时前
JAVA缓存的使用RedisCache、LocalCache、复合缓存
java·开发语言·缓存