LeetCode | 0040. Combination Sum II组合总和 II【Python】
2021-03-28 00:27
标签:val 路径 ack positive self from pru collect git LeetCode Given a collection of candidate numbers ( Each number in Note: Example 1: Example 2: 力扣 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用一次。 说明: 示例 1: 示例 2: 回溯模板 Python LeetCode | 0040. Combination Sum II组合总和 II【Python】 标签:val 路径 ack positive self from pru collect git 原文地址:https://www.cnblogs.com/wonz/p/13644304.htmlProblem
candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.candidates
may only be used once in the combination.
target
) will be positive integers.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]
]
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
问题
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
思路
res = []
def backtrack(路径, 选择列表):
if 满足结束条件:
res.append(路径)
return
for 选择 in 选择列表:
做选择
backtrack(路径, 选择列表)
撤销选择
Python3 代码
from typing import List
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
n = len(candidates)
if n == 0:
return []
# accelerate 剪枝提速,非必需
candidates.sort()
path, res = [], []
self.dfs(candidates, 0, n, path, res, target)
return res
def dfs(self, candidates, start, n, path, res, target):
# 1.valid result 递归终止情况
if target == 0:
res.append(path[:])
return
for i in range(start, n):
tmp = target - candidates[i]
# 3.pruning 剪枝
if tmp start and candidates[i] == candidates[i - 1]:
continue
# 2.backtrack and update 回溯以及更新 path
path.append(candidates[i])
self.dfs(candidates, i + 1, n, path, res, tmp)
path.pop()
GitHub 链接
上一篇:线程学习二
下一篇:python使用for生成列表
文章标题:LeetCode | 0040. Combination Sum II组合总和 II【Python】
文章链接:http://soscw.com/index.php/essay/68812.html