216. Combination Sum III -Medium

本文介绍了一种通过回溯算法解决寻找K个数字总和为n的所有可能组合的方法,其中数字范围限定在1到9之间,并且每个数字只能使用一次。

Question

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

找出K个数字总和为n的所有可能情况,给出的K个数字的大小在1-9之间,每个数字在一种情况中只能使用一次

Exameple

Input: k = 3, n = 7

Output: [[1,2,4]]


Input: k = 3, n = 9

Output: [[1,2,6], [1,3,5], [2,3,4]]

Solution

  • 回溯解。因为要列出所有情况,所以我们只要遍历所有可能性并且取出K个元素总和为n的情况即可。

    public class Solution {
        public List<List<Integer>> combinationSum3(int k, int n) {
            List<List<Integer>> res = new ArrayList<>();
            backtracking(1, k, n, res, new ArrayList<>());
            return res;
        }
    
        /**
         * 思路:遍历所有可能性并且取出K个元素总和为n的情况
         * @param start: 当前加入链表的元素索引
         * @param k:需要K个数
         * @param n:还需要达到的目标值
         * @param res:保存所有结果
         * @param temp:保存当前组合的链表
         */
        public void backtracking(int start, int k, int n, List<List<Integer>> res, List<Integer> temp){
            // 如果当前链表中的元素大于K个,直接返回
            if(k < temp.size()) return;
    
            // 如果K个元素的总和等于n,则添加到结果集并返回
            if(k == temp.size() && n == 0){
                // 拷贝一个链表,因为temp接下去还会被修改
                List<Integer> newTempList = new ArrayList<>(temp);
                res.add(newTempList);
                return;
            }
            // 回溯
            for(int i = start; i < 10; i++){
                temp.add(i);
                backtracking(i + 1, k,n - i, res, temp);
                // 每次使用后都清空使用过的元素
                temp.remove(temp.size() - 1);
            }
        }
    }
分析代码为什么不能够跑起来::;main_window.py 2025-09-22 22:12:01 - core.base_module - INFO - Utils module initialized 2025-09-22 22:12:01 - core.base_module - INFO - Project root: C:\Users\Administrator\Desktop\project 2025-09-22 22:12:01 - core - INFO - 核心模块导入成功 [模块状态] input_analysis.InputAnalysisModule: ✅ 已加载 2025-09-22 22:12:01 - DLT_Analysis - INFO - 主窗口初始化完成 2025-09-22 22:12:01 - InputAnalysis - INFO - 成功导入核心事件中心 2025-09-22 22:12:01 - InputAnalysis - INFO - 模块 input_analysis 已订阅 module_run 事件 2025-09-22 22:12:01 - root - INFO - 输入分析模块初始化完成: input_analysis [模块状态] combination_analysis.CombinationAnalysisModule: ✅ 已加载 2025-09-22 22:12:01 - modules.combination_analysis.combination_analysis - INFO - 组合分析模块初始化完成 [combination_analysis] 2025-09-22 22:12:01 - TrendAnalysis.trend_analysis - INFO - 初始化数据... 2025-09-22 22:12:01 - TrendAnalysis.trend_analysis - INFO - 开始读取文件: F:\超级\历史数据\历史数据.csv [模块状态] trend_analysis.TrendAnalysisModule: ✅ 已加载 [模块状态] number_generation.NumberGenerator: ✅ 已加载 2025-09-22 22:12:02 - TrendAnalysis.trend_analysis - INFO - 数据初始化完成,使用 2701 期数据 2025-09-22 22:12:02 - FollowAnalysisSystem.follow_analysis_engine - INFO - 模块 'follow_analysis_engine' 已订阅 module_run 事件 2025-09-22 22:12:02 - FollowAnalysisSystem.follow_analysis_engine - INFO - 初始化分析器... 2025-09-22 22:12:03 - FollowAnalysisSystem - INFO - 检测到文件编码: GB2312 (置信度: 99.0%) 2025-09-22 22:12:03 - FollowAnalysisSystem - INFO - 成功使用 GB2312 编码加载数据 2025-09-22 22:12:03 - FollowAnalysisSystem - INFO - 数据预处理完成,共 2701 期数据 2025-09-22 22:12:03 - FollowAnalysisSystem - INFO - 初始化分析器,使用 500 期数据 2025-09-22 22:12:03 - FollowAnalysisSystem.follow_analysis_engine - INFO - 分析器初始化完成,使用 2701 期数据 2025-09-22 22:12:03 - FollowAnalysisSystem.follow_analysis_engine - INFO - 模块 'follow_analysis_engine' 已注册,等待指令... 2025-09-22 22:12:03 - FollowAnalysisSystem.follow_analysis_engine - INFO - 数据文件路径: F:\超级\历史数据\历史数据.csv 2025-09-22 22:12:03 - InputAnalysis - INFO - 模块 input_analysis 已订阅 module_run 事件 2025-09-22 22:12:03 - root - INFO - 输入分析模块初始化完成: input_analysis 2025-09-22 22:12:03 - root - INFO - 成功创建模块: input_analysis (外部模块) 2025-09-22 22:12:03 - DLT_Analysis - INFO - 输入分析模块数据加载成功 2025-09-22 22:12:03 - DLT_Analysis - INFO - 收到数据提交 from main_ui: [] [模块状态] follow_analysis.FollowAnalysisModule: ✅ 已加载 2025-09-22 22:12:08 - DLT_Analysis - INFO - 输入分析模块数据加载成功 2025-09-22 22:12:08 - DLT_Analysis - INFO - 收到数据提交 from main_ui: [] 2025-09-22 22:12:10 - DLT_Analysis - INFO - Token已更新: 494e4b45-c80c-48f9-8ec9-df8ae5aed60f 2025-09-22 22:12:10 - root - ERROR - [号码池] 处理模块运行事件失败: 'dict' object has no attribute 'target' 2025-09-22 22:12:10 - root - INFO - 模块 input_analysis 分析执行成功 2025-09-22 22:12:10 - InputAnalysis - INFO - 收到运行指令: module_run 2025-09-22 22:12:10 - modules.number_generation - INFO - 收到运行指令: module_run 2025-09-22 22:12:10 - DLT_Analysis - WARNING - Token不匹配,忽略结果 2025-09-22 22:12:10 - InputAnalysis - INFO - 收到运行指令: module_run 2025-09-22 22:12:10 - InputAnalysis - INFO - 分析完成,结果已发布 2025-09-22 22:12:10 - DLT_Analysis - WARNING - Token不匹配,忽略结果 2025-09-22 22:12:10 - InputAnalysis - INFO - 分析完成,结果已发布 2025-09-22 22:12:12 - modules.combination_analysis.combination_analysis - INFO - 组合分析模块初始化完成 [combination_analysis] 2025-09-22 22:12:12 - root - INFO - 成功创建模块: combination_analysis (外部模块) 2025-09-22 22:12:13 - DLT_Analysis - INFO - Token已更新: bbc353cc-ee39-44f7-a5cc-c48b4e944e36 2025-09-22 22:12:13 - root - ERROR - [号码池] 处理模块运行事件失败: 'dict' object has no attribute 'target' 2025-09-22 22:12:13 - root - ERROR - 处理模块运行事件失败: not enough values to unpack (expected 2, got 1) 2025-09-22 22:12:13 - modules.number_generation - INFO - 收到运行指令: module_run 2025-09-22 22:12:14 - FollowAnalysisSystem.follow_analysis - INFO - 模块 'follow_analysis' 已订阅 module_run 事件 2025-09-22 22:12:14 - FollowAnalysisSystem.follow_analysis - INFO - 初始化分析器... 2025-09-22 22:12:15 - FollowAnalysisSystem - INFO - 检测到文件编码: GB2312 (置信度: 99.0%) 2025-09-22 22:12:15 - FollowAnalysisSystem - INFO - 成功使用 GB2312 编码加载数据 2025-09-22 22:12:15 - FollowAnalysisSystem - INFO - 数据预处理完成,共 2701 期数据 2025-09-22 22:12:15 - FollowAnalysisSystem - INFO - 初始化分析器,使用 500 期数据 2025-09-22 22:12:15 - FollowAnalysisSystem.follow_analysis - INFO - 分析器初始化完成,使用 2701 期数据 2025-09-22 22:12:15 - FollowAnalysisSystem.follow_analysis - INFO - 模块 'follow_analysis' 已注册,等待指令... 2025-09-22 22:12:15 - FollowAnalysisSystem.follow_analysis - INFO - 数据文件路径: F:\超级\历史数据\历史数据.csv 2025-09-22 22:12:15 - root - INFO - 成功创建模块: follow_analysis (外部模块) 2025-09-22 22:12:16 - DLT_Analysis - INFO - Token已更新: 7c6bbeb0-ff4b-408a-b869-770418fad0eb 2025-09-22 22:12:16 - root - ERROR - [号码池] 处理模块运行事件失败: 'dict' object has no attribute 'target' 2025-09-22 22:12:16 - root - WARNING - 模块 follow_analysis 没有可执行的方法 2025-09-22 22:12:16 - modules.number_generation - INFO - 收到运行指令: module_run 2025-09-22 22:12:16 - FollowAnalysisSystem.follow_analysis - INFO - 收到来自 main_ui 的运行指令,token=7c6bbeb0-ff4b-408a-b869-770418fad0eb 2025-09-22 22:12:16 - FollowAnalysisSystem - INFO - 初始化分析器,使用 500 期数据 2025-09-22 22:12:16 - FollowAnalysisSystem - INFO - 跟随分析完成,前区 35 种,后区 12 种跟随关系 2025-09-22 22:12:16 - FollowAnalysisSystem - INFO - 跟随分析完成,前区 35 种,后区 12 种跟随关系 2025-09-22 22:12:17 - FollowAnalysisSystem - INFO - 跟随分析完成,前区 35 种,后区 12 种跟随关系 2025-09-22 22:12:17 - FollowAnalysisSystem - INFO - 跟随分析完成,前区 35 种,后区 12 种跟随关系 2025-09-22 22:12:17 - FollowAnalysisSystem.follow_analysis - INFO - 发送分析结果... 2025-09-22 22:12:17 - DLT_Analysis - WARNING - Token不匹配,忽略结果 2025-09-22 22:12:17 - FollowAnalysisSystem.follow_analysis - INFO - 结果已发送到: main_ui 2025-09-22 22:12:17 - FollowAnalysisSystem.follow_analysis - INFO - 分析完成,结果已发送 2025-09-22 22:12:18 - TrendAnalysis.trend_analysis - INFO - 初始化数据... 2025-09-22 22:12:18 - TrendAnalysis.trend_analysis - INFO - 开始读取文件: F:\超级\历史数据\历史数据.csv 2025-09-22 22:12:18 - TrendAnalysis.trend_analysis - INFO - 数据初始化完成,使用 2701 期数据 2025-09-22 22:12:18 - root - INFO - 成功创建模块: trend_analysis (外部模块) 2025-09-22 22:12:19 - DLT_Analysis - INFO - Token已更新: 16968b10-35ea-4f2e-9f25-3505e1ad4cb8 2025-09-22 22:12:19 - root - ERROR - [号码池] 处理模块运行事件失败: 'dict' object has no attribute 'target' 2025-09-22 22:12:19 - root - ERROR - 处理模块运行事件失败: TrendAnalysisModule.run_analysis() takes 1 positional argument but 2 were given 2025-09-22 22:12:19 - modules.number_generation - INFO - 收到运行指令: module_run 2025-09-22 22:12:21 - root - INFO - 成功创建模块: number_generation (外部模块) 2025-09-22 22:12:22 - DLT_Analysis - INFO - Token已更新: bbb93eb3-6f6b-4916-be3f-32f2d7eca4a0 2025-09-22 22:12:22 - root - ERROR - [号码池] 处理模块运行事件失败: 'dict' object has no attribute 'target' 2025-09-22 22:12:22 - root - WARNING - 模块 number_generation 没有可执行的方法 2025-09-22 22:12:22 - modules.number_generation - INFO - 收到运行指令: module_run 2025-09-22 22:12:22 - modules.number_generation - INFO - 收到运行指令: module_run 进程已结束,退出代码为 0
最新发布
09-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值