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

【Leetcode 每日一题】40. 组合总和 II

问题背景

给定一个候选人编号的集合 c a n d i d a t e s candidates candidates 和一个目标数 t a r g e t target target,找出 c a n d i d a t e s candidates candidates 中所有可以使数字和为 t a r g e t target target 的组合。
c a n d i d a t e s candidates candidates 中的每个数字在每个组合中只能使用 一次
注意:解集不能包含重复的组合。

数据约束

  • 1 ≤ c a n d i d a t e s . l e n g t h ≤ 100 1 \le candidates.length \le 100 1candidates.length100
  • 1 ≤ c a n d i d a t e s [ i ] ≤ 50 1 \le candidates[i] \le 50 1candidates[i]50
  • 1 ≤ t a r g e t ≤ 30 1 \le target \le 30 1target30

解题过程

算是回溯的例题,由于结果不能重复,要先将数组进行排序,方便在过程中跳已经处理过的相等元素。

具体实现

递归

class Solution {public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);List<List<Integer>> res = new ArrayList<>();List<Integer> path = new ArrayList<>();dfs(0, target, candidates, res, path);return res;}private void dfs(int i, int target, int[] candidates, List<List<Integer>> res, List<Integer> path) {if(target == 0) {res.add(new ArrayList(path));return;}if(i == candidates.length) {return;}int cur = candidates[i];if(target < cur) {return;}path.add(cur);dfs(i + 1, target - cur, candidates, res, path);path.remove(path.size() - 1);i++;while(i < candidates.length && candidates[i] == cur) {i++;}dfs(i, target, candidates, res, path);}
}

递推

class Solution {public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);List<List<Integer>> res = new ArrayList<>();List<Integer> path = new ArrayList<>();dfs(0, target, candidates, res, path);return res;}private void dfs(int i, int target, int[] candidates, List<List<Integer>> res, List<Integer> path) {if(target == 0) {res.add(new ArrayList<>(path));return;}for (int j = i; j < candidates.length && candidates[j] <= target; j++) {if(j > i && candidates[j] == candidates[j - 1]) {continue;}path.add(candidates[j]);dfs(j + 1, target - candidates[j], candidates, res, path);path.remove(path.size() - 1);}}
}
http://www.lryc.cn/news/527277.html

相关文章:

  • python 变量范围的定义与用法
  • TRTC实时对话式AI解决方案,助力人机语音交互极致体验
  • dev c++ ‘unordered_set‘ does not name a type
  • 算法每日双题精讲 —— 二分查找(寻找旋转排序数组中的最小值,点名)
  • three.js+WebGL踩坑经验合集(4.2):为什么不在可视范围内的3D点投影到2D的结果这么不可靠
  • Kafka运维宝典 (二)- kafka 查看kafka的运行状态、broker.id不一致导致启动失败问题、topic消息积压量告警监控脚本
  • 全球AI模型百科全书,亚马逊云科技Bedrock上的100多款AI模型
  • 微信小程序中常见的 跳转方式 及其特点的表格总结(wx.navigateTo 适合需要返回上一页的场景)
  • 【Elasticsearch】index:false
  • 新版IDEA创建数据库表
  • 输入带空格的字符串,求单词个数
  • C语言程序设计十大排序—希尔排序
  • Excel制作合同到期自动提醒!
  • “AI质量评估系统:智能守护,让品质无忧
  • 爬虫基础之爬取某基金网站+数据分析
  • 使用 Aryn DocPrep、DocParse 和 Elasticsearch 向量数据库实现高质量 RAG
  • Couchbase UI: Server
  • Web3.0时代的挑战与机遇:以开源2+1链动模式AI智能名片S2B2C商城小程序为例的深度探讨
  • langchain基础(一)
  • 【Android】布局文件layout.xml文件使用控件属性android:layout_weight使布局较为美观,以RadioButton为例
  • RabbitMQ 架构分析
  • Qt Enter和HoverEnter事件
  • 大语言模型之prompt工程
  • WPF基础 | WPF 常用控件实战:Button、TextBox 等的基础应用
  • [笔记] 极狐GitLab实例 : 手动备份步骤总结
  • 随笔十七、eth0单网卡绑定双ip的问题
  • 逻辑复制parallel并发参数测试
  • Cursor 帮你写一个小程序
  • WordPress免费证书插件
  • Linux:多线程[2] 线程控制