python回溯模板

数字为力扣题号

#39 组合总和
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]

candidates.sort()
n = len(candidates)
ans = []
path = []
def backtrack(candidates,target,sums,Index):
    if sums == target:
        ans.append(path[:])
    for i in range(Index,n):
        if sums + candidates[i] > target:
            return
        sums += candidates[i]       
        path.append(candidates[i])
        backtrack(candidates,target,sums,i)
        sums -= candidates[i]
        path.pop()

backtrack(candidates,target,0,0)
return ans

#40 组合总和 II
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[[1,1,6],[1,2,5],[1,7],[2,6]]

candidates.sort()
n = len(candidates)
ans = []
path = []
def backtrack(candidates,target,sums,Index):
    if sums == target:
        ans.append(path[:])
    for i in range(Index,n):
        if sums + candidates[i] > target:
            return
        if i > Index and candidates[i] == candidates[i-1]:  #去重
            continue
        sums += candidates[i]
        path.append(candidates[i])
        backtrack(candidates,target,sums,i+1) #遍历下一个数
        sums -= candidates[i]
        path.pop()
backtrack(candidates,target,0,0)
return ans

#46 全排列
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

ans = []
path = []
def backtrack(path,choices):
    if len(path) == len(nums):
        ans.append(path[:])

    for i in choices:
        if i in path:
            continue
        path.append(i)
        backtrack(path,choices)
        path.pop()
    return ans
backtrack([],nums)
return ans

#47 全排列 II
输入:nums = [1,1,2]
输出:
[[1,1,2][1,2,1],[2,1,1]]

if not nums: return []
res = []
used = [0] * len(nums)
def backtracking(nums, used, path):
    if len(path) == len(nums):
        res.append(path.copy())
        return
    for i in range(len(nums)):
        if not used[i]:
            if i>0 and nums[i] == nums[i-1] and not used[i-1]:
                continue
            used[i] = 1
            path.append(nums[i])
            backtracking(nums, used, path)
            path.pop()
            used[i] = 0
backtracking(sorted(nums),used,[])
return res

#77
输入:n = 4, k = 2
输出:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]]

ans = []
path = []
nums = [i for i in range(1,n+1)]
def backtrack(nums,Index):
    if len(path) == k:
        ans.append(path[:])
    for i in range(Index,len(nums)):
        path.append(nums[i])
        backtrack(nums,i+1)
        path.pop()
    return ans
backtrack(nums,0)
return ans

#78 子集
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

res = []  
path = []  
def backtrack(nums,startIndex):
    res.append(path[:])
    for i in range(startIndex,len(nums)):
        path.append(nums[i])
        backtrack(nums,i+1)
        path.pop()
backtrack(nums,0)
return res

#90 子集 II
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

res = []  
path = [] 
def backtrack(nums,startIndex):
    res.append(path[:])
    for i in range(startIndex,len(nums)):
        if i > startIndex and nums[i] == nums[i - 1]:
            continue
        path.append(nums[i])
        backtrack(nums,i+1) 
        path.pop()
    return res
nums = sorted(nums)
backtrack(nums,0)
return res

猜你喜欢

转载自blog.csdn.net/qq_46500711/article/details/124695038