回溯算法本质是DFS的一种,先选择一条路一直走到底 发现不符合要求了再返回 在寻路的过程中, 如果可以提前发现不符合要求 ,则提前终止 即为剪枝
78.子集问题
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
第一种形式:依次对于每个元素 我们都可以选取或者不选取,但是都要将index加1 而探路的终止条件就是index==len(nums)。在这种思路下 空集【】即为三个元素都不选取 但是他的index为3 所有是一个解
class Solution:
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def DFS(nums,s,index):
#终止条件
if len(nums) == index:
res.append(s)
return
DFS(nums,s+[nums[index]],index+1) #选取当前元素
DFS(nums,s,index+1)#不选取当前元素
res = []
s = []
DFS(nums,s,0)
return res
第二种形式,可以用循环体代替我们选与不选的操作 每次进入新的一层循环 i+=1 所以最多进入三层
每次进入一个新层相当于选择当前元素 而进入下一个for循环相当于不选择当前元素
不同点是此方法是每选择一个元素 就生成一次子集 而第一种是当选择次数到了3后在加入子集
因为选过此元素后会进入下一个for循环 所有不会有重复选择
class Solution:
def subsets(self, nums):
if not nums:
return []
res = []
n = len(nums)
def helper(idx, temp_list):
res.append(temp_list)
for i in range(idx, n):
helper(i + 1, temp_list + [nums[i]])
helper(0, [])
return res
39 组合总和
给定一个数组 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]
]
第一种方式,同样对于每个元素 我们都可以选取或者不选取,而终止条件变为此时的sum==target所以每次循环时要传入sum变量 由于可以重复选取 所有当选择此元素后 新的层还可以选择此元素
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
n = len(candidates)
res = []
def helper(i, tmp_sum, tmp):
if tmp_sum > target or i == n:
return
if tmp_sum == target:
res.append(tmp)
return
helper(i, tmp_sum + candidates[i],tmp + [candidates[i]])#选择当前元素
helper(i+1, tmp_sum ,tmp)#不选择当前元素
helper(0, 0, [])
return res
第二种方式 思路一样 只是用循环体来表示
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
n = len(candidates)
res = []
def backtrack(i, tmp_sum, tmp):
if tmp_sum > target or i == n:
return
if tmp_sum == target:
print(tmp)
res.append(tmp)
return
for j in range(i, n):
print(j,tmp_sum,tmp)
if tmp_sum + candidates[j] > target:
break
backtrack(j,tmp_sum + candidates[j],tmp+[candidates[j]])
backtrack(0, 0, [])
return res
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]
]
第一种方式,由于有重复的元素 所以要考虑去重 第一种方式考虑在每次添加元素时进行去重
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if(not candidates):
return []
n=len(candidates)
candidates.sort()
res=[]
def helper(j,tmp,sum1):
if(j==n or sum1>target):
#print(sum1)
return
if sum1 == target:
if tmp not in res:
res.append(tmp)
#print(tmp)
return
if j+1 ==n and sum1+candidates[j] ==target: #为了特判【1,1,1】target=3 这种特殊情况
if tmp+[candidates[j]] not in res:
res.append(tmp+[candidates[j]])
if candidates[j] == target: #由于此方法不能再次选取自己 对应末尾的等于targe的元素会判断不了 所以加一个特判
if [candidates[j]] not in res:
res.append([candidates[j]])
#print(tmp)
return
helper(j+1,tmp+[candidates[j]],sum1+candidates[j])
helper(j+1,tmp,sum1)
helper(0,[],0)
return res
此题推荐用第二种方式 因为用循环体可以方便的进行去重 首先将数组排序
如果发现排序后两个相邻元素相同 直接continue 不对当前元素进行操作
if j > i and candidates[j] == candidates[j-1]:
continue
完整代码如下
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates:
return []
candidates.sort()
n = len(candidates)
res = []
def backtrack(i, tmp_sum, tmp_list):
if tmp_sum == target:
res.append(tmp_list)
return
for j in range(i, n):
if tmp_sum + candidates[j] > target : break
if j > i and candidates[j] == candidates[j-1]:continue
backtrack(j + 1, tmp_sum + candidates[j], tmp_list + [candidates[j]])
backtrack(0, 0, [])
return res
先介绍这么多 还有一些回溯问题下次再讲