力扣 中等 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;
        }
    }
}
相关推荐
雨白1 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD3 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859573 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta19984 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油4 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero5 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称5 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划
Felven5 小时前
B. Fair Numbers
数据结构·算法
人道领域5 小时前
【LeetCode刷题日记】93.复原IP地址
java·开发语言·算法·leetcode