ii 组合总和_40. 组合总和 II

题目描述

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:所有数字(包括目标数)都是正整数。

解集不能包含重复的组合。

示例 1:输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]

示例 2:输入: candidates = [2,5,2,1,2], target = 5,所求解集为:[  [1,2,2],  [5]]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/combination-sum-ii

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这道题目是求集合,并不是 求极值,因此动态规划不是特别切合,因此我们需要考虑别的方法。

这种题目其实有一个通用的解法,就是回溯法。网上也有大神给出了这种回溯法解题的 通用写法,这里的所有的解法使用通用方法解答。除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。

我们先来看下通用解法的解题思路,我画了一张图:

通用写法的具体代码见下方代码区。

关键点解析回溯法

backtrack 解题公式

代码语言支持: Javascript,Python3/* * @lc app=leetcode id=40 * * [40] Combination Sum II * * https://leetcode.com/problems/combination-sum-ii/description/ * * algorithms * Medium (40.31%) * Total Accepted:    212.8K * Total Submissions: 519K * Testcase Example:  '[10,1,2,7,6,1,5]\n8' * * Given a collection of candidate numbers (candidates) and a target number * (target), find all unique combinations in candidates where the candidate * numbers sums to target. * * Each number in candidates may only be used once in the combination. * * Note: * * * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. * * * Example 1: * * * Input: candidates = [10,1,2,7,6,1,5], target = 8, * A solution set is: * [ * ⁠ [1, 7], * ⁠ [1, 2, 5], * ⁠ [2, 6], * ⁠ [1, 1, 6] * ] * * * Example 2: * * * Input: candidates = [2,5,2,1,2], target = 5, * A solution set is: * [ * [1,2,2], * [5] * ] * * */function backtrack(list, tempList, nums, remain, start) { if (remain  start && nums[i] == nums[i-1]) continue; // skip duplicates      tempList.push(nums[i]);      backtrack(list, tempList, nums, remain - nums[i], i + 1); // i + 1代表不可以重复利用, i 代表数字可以重复使用      tempList.pop(); } }/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */var combinationSum2 = function(candidates, target) { const list = [];    backtrack(list, [], candidates.sort((a, b) => a - b), target, 0); return list;};

Python3 Code:class Solution:

defcombinationSum2(self,candidates: List[int],target:int) -> List[List[int]]:

"""

与39题的区别是不能重用元素,而元素可能有重复;

不能重用好解决,回溯的index往下一个就行;

元素可能有重复,就让结果的去重麻烦一些;

"""

size=len(candidates)

ifsize== 0:

return []

# 还是先排序,主要是方便去重

candidates.sort()

path= []

res= []

self._find_path(candidates,path,res,target, 0,size)

returnres

def_find_path(self,candidates,path,res,target,begin,size):

iftarget== 0:

res.append(path.copy())

else:

foriinrange(begin,size):

left_num=target-candidates[i]

ifleft_num< 0:

break

# 如果存在重复的元素,前一个元素已经遍历了后一个元素与之后元素组合的所有可能

ifi>beginandcandidates[i] ==candidates[i-1]:

continue

path.append(candidates[i])

# 开始的 index 往后移了一格

self._find_path(candidates,path,res,left_num,i+1,size)

path.pop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值