力扣 中等 216组合总和III

文章目录

题目介绍

解法

是77.组合链接的扩展

java 复制代码
class Solution {
    List<List<Integer>> result= new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int n, int k) {
        dfs(n, k, 1, 0);
        return result;
    }
    public void dfs(int targetSum, int k, int startIndex, int sum){
        if (path.size() == k){
            if (sum == targetSum){
                result.add(new ArrayList<>(path));
            }
            return;
        }
        for (int i = startIndex;i <= 9;i++){
            path.add(i);
            sum += i;
            dfs(targetSum, k, i + 1, sum);
            path.remove(path.size() - 1);
            sum -= i;
        }
    }
}
相关推荐
M78佐菲20 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
AI备案指南-满满1 天前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案
土司大王1 天前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背1 天前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
zander2581 天前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲1 天前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力551 天前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜1 天前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者1 天前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法