dfs --path sum 问题 本质上就是组合问题(有去重)
135. 数字组合
中文
English
给定一个候选数字的集合 candidates
和一个目标值 target
. 找到 candidates
中所有的和为 target
的组合.
在同一个组合中, candidates
中的某个数字不限次数地出现.
样例
样例 1:
输入: candidates = [2, 3, 6, 7], target = 7
输出: [[7], [2, 2, 3]]
样例 2:
输入: candidates = [1], target = 3
输出: [[1, 1, 1]]
注意事项
- 所有数值 (包括
target
) 都是正整数. - 返回的每一个组合内的数字必须是非降序的.
- 返回的所有组合之间可以是任意顺序.
- 解集不能包含重复的组合.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Solution: """ @param candidates: A list of integers @param target: An integer @return: A list of lists of integers """ def combinationSum( self , candidates, target): # write your code here result = [] self .dfs( sorted ( list ( set (candidates))), target, result, path = [], start_index = 0 ) return result def dfs( self , nums, target, result, path, start_index): if target = = 0 and path: result.append( list (path)) if target < 0 : return for i in range (start_index, len (nums)): path.append(nums[i]) self .dfs(nums, target - nums[i], result, path, i) path.pop() |
153. 数字组合 II
中文
English
给定一个数组 num
和一个整数 target
. 找到 num
中所有的数字之和为 target
的组合.
样例
样例 1:
输入: num = [7,1,2,5,1,6,10], target = 8
输出: [[1,1,6],[1,2,5],[1,7],[2,6]]
样例 2:
输入: num = [1,1,1], target = 2
输出: [[1,1]]
解释: 解集不能包含重复的组合
注意事项
- 在同一个组合中,
num
中的每一个数字仅能被使用一次. - 所有数值 (包括
target
) 都是正整数. - 返回的每一个组合内的数字必须是非降序的.
- 返回的所有组合之间可以是任意顺序.
- 解集不能包含重复的组合.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Solution: """ @param num: Given the candidate numbers @param target: Given the target number @return: All the combinations that sum to target """ def combinationSum2( self , nums, target): # write your code here result = [] self .dfs( sorted (nums), target, result, path = [], start_index = 0 ) return result def dfs( self , nums, target, result, path, start_index): if target = = 0 and path: result.append( list (path)) if target < 0 : return for i in range (start_index, len (nums)): if i > 0 and nums[i] = = nums[i - 1 ] and i > start_index: continue path.append(nums[i]) self .dfs(nums, target - nums[i], result, path, i + 1 ) path.pop() |
90. k数和 II
中文
English
给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字。
在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案。
样例
样例 1:
输入: [1,2,3,4], k = 2, target = 5
输出: [[1,4],[2,3]]
样例 2:
输入: [1,3,4,6], k = 3, target = 8
输出: [[1,3,4]]
这个题目更简单,直接dfs模板即可做。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Solution: """ @param: A: an integer array @param: k: a postive integer <= length(A) @param: targer: an integer @return: A list of lists of integer """ def kSumII( self , A, k, target): # write your code here path = [] result = [] self .dfs(A, k, target, path, result, start_index = 0 ) return result def dfs( self , arr, k, target, path, result, start_index): if target = = 0 and k = = 0 : result.append( list (path)) return if target < 0 or k < = 0 : return for i in range (start_index, len (arr)): path.append(arr[i]) self .dfs(arr, k - 1 , target - arr[i], path, result, i + 1 ) path.pop() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」