力扣 中等 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;
        }
    }
}
相关推荐
文丑颜不良啊2 小时前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊3 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊4 小时前
LeetCode2521~2540题目解析
java·leetcode
lisw055 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术6 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs7 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057597 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
文丑颜不良啊7 小时前
LeetCode2501~2520题目解析
java·leetcode
GoAI18 小时前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试