- 题目1:给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。你可以按 任何顺序 返回答案。
示例 1:
输入:n = 4, k = 2
输出:
[[2,4], [3,4],[2,3],[1,2],[1,3],[1,4],]
题目链接:https://leetcode-cn.com/problems/combinations/
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
path = []
res = []
startindex = 1
def backtracking(n, k, startindex):
if len(path) == k:
res.append(path[:])
return
for i in range(startindex, n + 2 - (k -len(path))): # 剪枝
path.append(i)
backtracking(n, k, i + 1)
path.pop()
backtracking(n, k, startindex)
return res
- 题目2:给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按任意顺序返回这些组合。
candidates 中的同一个 数字可以无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合
题目链接:https://leetcode-cn.com/problems/combination-sum/
# 写法一:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
path = []
def backtracking(candidates, target, startindex, sum_):
if sum_ == target:
res.append(path[:])
return
for i in range(startindex, len(candidates)):
# 这样写原数组candidates可以不用排序,因为它会在本层遍历所有的数
# 如果有符合条件的就继续,如果都不符合就回溯到上一层遍历
if sum_ + candidates[i] <= target:
sum_ += candidates[i]
path.append(candidates[i])
# 可以重复使用,所以到下一层时还是可以从下标为i的地方开始取数
backtracking(candidates, target, i, sum_)
sum_ -= candidates[i]
path.pop()
backtracking(candidates, target, 0, 0)
return res
# 写法二:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
path = []
def backtracking(candidates, target, startindex, sum_):
if sum_ == target:
res.append(path[:])
return
for i in range(startindex, len(candidates)):
# 这样写原数组必须要从小到大排序,当 sum_ + 下一个数大于target时,
# 说明后面的所有数 + sum_都大于target,这时就可以停止本层的遍历,直接回溯到上一层遍历
if sum_ + candidates[i] > target: return
sum_ += candidates[i]
path.append(candidates[i])
# 可以重复使用,所以到下一层时还是可以从下标为i的地方开始取数
backtracking(candidates, target, i, sum_)
sum_ -= candidates[i]
path.pop()
candidates.sort() # 必须对数组排序
backtracking(candidates, target, 0, 0)
return res
"""
这两种写法都进行了剪枝,但是具体写法不同
方法二: 对原数组从小到大排序,当 sum_ + 下一个数大于target时,
说明后面的所有数 + sum_都大于target,这时就可以停止本层的遍历,直接回溯到上一层遍历
方法一:若是返回上一层,必须对本层数据都进行判断,如果途中有符合条件的数,则会进入下一层,若是都不
符合才会返回上一层,
假设我们已知本层数据都不符合条件,但对于计算机来说并不知道所以它必须都判断一次
然后才能返回上一层
对比:方法二是花费一个排序的时间,然后在后续的每层遍历中赢得时间
方法一是省了一个排序的时间,但是在后续的每层遍历中花费了时间
总的来说写法二更优
方法一我写的,方法二别人写的!(T-T)
"""
-
题目3:给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:[[1,1,6],[1,2,5],[1,7],[2,6]]
题目链接:https://leetcode-cn.com/problems/combination-sum-ii/
class Solution:
def combinationSum2(self, candidates, target):
path = []
res = []
def backtracking(candidates, target, startindex, sum_):
if sum_ == target:
res.append(path[:])
return
for i in range(startindex, len(candidates)):
if sum_ + candidates[i] > target: return
# 判断在本层是否使用过
if i > startindex and candidates[i] == candidates[i-1]: continue
sum_ += candidates[i]
path.append(candidates[i])
# 不可以重复使用,所以到下一层时i不能取要从下标为i+1的地方开始取数
backtracking(candidates, target, i + 1, sum_)
sum_ -= candidates[i]
path.pop()
candidates.sort() # 这里必须排序
backtracking(candidates, target, 0, 0)
return res
"""
这道题与上一道的区别在于:
上一道数组中数不重复,而且可以重复取用
这道数组中的数有重复,而且不可以重复取用
这道题先进行排序,这样一样的数据就会挨在一起,然后每层遍历时,判断在本层是否使用过该数字
例如 1 1 1 2 2 2 3 3 3
第一层取 1 ↙ ↘ 但是回溯到第一层遍历时不能再取1,只能从2开始
剩余 1 1 2 2 2 3 3 3
第二层还可以取 1 ↙ ↘但是回溯到第二层遍历时不能再取1,因为本层已经取过只能从2开始
剩余 1 2 2 2 3 3 3
还可以取1 ↙取2↘ ↘ 第2次回溯到本层遍历后续数字时,不能再取2,
↙本层第一次取2 因为第1次回溯到本层已经用过一次
略 ↙
"""
这个题目描述不好理解,大家可以参考
官方和各路神仙的题解:https://leetcode-cn.com/problems/combination-sum-ii/solution/