当前位置: 首页 > news >正文

058组合总和

组合总和题目链接https://leetcode.cn/problems/combination-sum/description/?envTypestudy-plan-v2envIdtop-100-liked我的解答public ListListInteger combinationSum(int[] candidates, int target) { ListListInteger ans new ArrayList(); dfs(candidates, target, 0, 0, new ArrayList(), ans); return ans; } //startIndex每次选取的初始位置我们只需让下一次选取从上一次选取的位置开始就可避免出现重复组合 public void dfs(int[] candidates, int target, int sum, int startIndex, ListInteger output, ListListInteger ans){ if(sum target){ ans.add(new ArrayList(output)); return; } if(sum target){ return; } int length candidates.length; for(int istartIndex; ilength; i){ sum candidates[i]; output.add(candidates[i]); dfs(candidates, target, sum, i, output, ans); sum - candidates[i]; output.remove(output.size()-1); } }分析​ 代码的时间复杂度为O(S)其中 S 为所有可行解的长度之和。空间复杂度为O(target)除答案数组外空间复杂度取决于递归的栈深度在最差情况下需要递归 O(target) 层。本题复杂度我不知道该如何表示直接看了官方的​ 解题思路采用基本的递归 回溯方法唯一的难点在于如何避免出现重复组合经过思考我发现由于candidates是一个无重复元素的整数数组所以只需让下一次选取从上一次选取的位置开始就可以避免出现重复组合。看了官方题解后的解答//方法搜索回溯官方版 //时间复杂度O(S)其中 S 为所有可行解的长度之和。 //空间复杂度O(target)除答案数组外空间复杂度取决于递归的栈深度在最差情况下需要递归 O(target) 层。 public ListListInteger combinationSum(int[] candidates, int target) { ListListInteger ans new ArrayListListInteger(); ListInteger combine new ArrayListInteger(); dfs(candidates, target, ans, combine, 0); return ans; } public void dfs(int[] candidates, int target, ListListInteger ans, ListInteger combine, int idx) { if (idx candidates.length) { return; } if (target 0) { ans.add(new ArrayListInteger(combine)); return; } // 直接跳过 dfs(candidates, target, ans, combine, idx 1); // 选择当前数 if (target - candidates[idx] 0) { combine.add(candidates[idx]); dfs(candidates, target - candidates[idx], ans, combine, idx); combine.remove(combine.size() - 1); } } //方法搜索回溯看了官方解答后对我的解答进行优化版 //时间复杂度O(S)其中 S 为所有可行解的长度之和。 //空间复杂度O(target)除答案数组外空间复杂度取决于递归的栈深度在最差情况下需要递归 O(target) 层。 public ListListInteger combinationSum(int[] candidates, int target) { ListListInteger ans new ArrayList(); dfs(candidates, target, 0, new ArrayList(), ans); return ans; } //startIndex每次选取的初始位置我们只需让下一次选取从上一次选取的位置开始就可避免出现重复组合同时进行剪枝 public void dfs(int[] candidates, int target, int startIndex, ListInteger output, ListListInteger ans){ if(target 0){ ans.add(new ArrayList(output)); return; } int length candidates.length; for(int istartIndex; ilength; i){ if(target - candidates[i] 0){ target - candidates[i]; output.add(candidates[i]); dfs(candidates, target, i, output, ans); target candidates[i]; output.remove(output.size()-1); } } }分析官方的解题思路与我的解答基本一致但是在剪枝上进行了优化优化的点在于每次判断target减去当前选取的数后是否小于0若小于0则直接跳过这个数。总结本题采用基本的递归 回溯方法思路简单唯一需要思考的是如何进一步进行剪枝优化。
http://www.gsyq.cn/news/1380164.html

相关文章:

  • 微信小程序抓包实战:Yakit与Fiddler协同调试指南
  • LLM Structured Output 生产工程:别再写正则解析JSON 了(工程师踩坑版)
  • LeetCode 80 · 删除有序数组中的重复项 II:通用模板的威力
  • HybridCLR-Unity原生C#热更新终极方案
  • 终极城通网盘解析指南:3分钟获取高速直连下载地址的完整教程
  • 开源HR系统OpenHRMS:如何用模块化设计破解企业人事管理难题?
  • 财务怎么做经营分析?一文说清经营分析的9大体系30个指标!
  • 百联 OK 卡安全高效变现指南 - 购物卡回收找京尔回收
  • 数据挖掘是什么?数据分析、数据挖掘、数据统计三者的区别是什么
  • Skeptical Learning:人机协作式数据清洗框架的原理、实践与挑战
  • Obsidian PDF++解决方案:构建原生双向链接的知识管理生态系统
  • Taotoken 的用量看板与成本管理功能如何帮助团队控制 AI 支出
  • 【分享】AIDE Pro 制作属于自己的手机软件
  • XUnity自动翻译工具:如何让外语游戏瞬间变成你的母语版本?
  • 【稀缺首发】PlayAI首次开放评测接口权限!但我们已逆向解析其质量打分逻辑,并构建第三方可信验证框架
  • NLP —— Transformers库使用
  • taotoken模型广场功能详解与模型选型决策指南
  • 2026年厂区节能减排公司有哪些?工业能源托管与余热回收系统厂家实力推荐 - 品牌2025
  • 告别英文界面:Cobalt Strike 4.8 保姆级汉化安装与首次连接指南
  • WPF中Style和ControlTemplate的触发器有什么不同
  • 企业内统一AI开发环境借助Taotoken CLI实现快速配置
  • 项目文档:基于51单片机的篮球计分器设计
  • 用Icarus Verilog破解数字电路调试困局的实战心法
  • request接口调用的三种方法(1)
  • qobuz-dl 终极指南:如何轻松下载无损音乐建立个人高品质音乐库
  • sd卡分区了数据还能恢复吗,只需3种方法和视频教学,数据就能神奇地回来!
  • AI 分析重构(AI-Assisted Refactoring)详解
  • 济南黄金回收怎么选?福运来人气与口碑双冠 - 黄金回收
  • 音乐格式转换终极指南:3步解锁所有加密音频
  • 原神自动化助手GIS:3大核心功能彻底解放你的双手