力扣 中等 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;
        }
    }
}
相关推荐
文丑颜不良啊18 小时前
LeetCode2981~3000题目解析
java·leetcode
罗西的思考19 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(2)
人工智能·算法·机器学习
罗西的思考19 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(1)
人工智能·算法·机器学习
武子康21 小时前
H3-World 接入 Game Agent:谁来判断门真的打开了?
人工智能·算法·机器学习
文丑颜不良啊1 天前
LeetCode3001~3020题目解析
java·leetcode
武子康1 天前
H3-World 扩展跳跃与开门:新动作怎样进入训练数据?
大数据·人工智能·算法
卡尔曼的BD SLAMer1 天前
北斗导航 |GNSS ION论文集汇总1997到2022(链接在文末)
算法·北斗导航
时间的拾荒人1 天前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
老师我太想进步了20261 天前
【数据结构-8】栈的实现
c语言·数据结构·算法
kyle~1 天前
因子图优化(Factor Graph Optimization)
c++·算法·机器人·传感器