
回溯
BlackMan_阿伟
不积跬步无以至千里
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
April——面试题
题目描述:给定一个数组,都是整数,比如[2,2,3,4],判断数组中的数字能组成多少种三角形?#给定一个数组,判断组成三角形的有多少种组合def help(nums): a,b,c = nums[0],nums[1],nums[2] if a+b>c and a+c>b and c+b>a and a-b<c and a-c<b and c-b<a: return True return Falsedef huisu(i原创 2021-04-23 21:41:57 · 104 阅读 · 0 评论 -
March——131.分割回文串(回溯算法)
class Solution: def partition(self, s: str) -> List[List[str]]: if len(s)==0: return [[]] if len(s)==1: return [[s]] isPalindrome = lambda s : s == s[::-1] def backtrac...原创 2021-03-08 22:12:20 · 159 阅读 · 0 评论 -
《Leetcode of September 》784. 字母大小写全排列
class Solution: def letterCasePermutation(self, S: str) -> List[str]: res = [] def huisu(S,index): res.append(S) for i in range(index,len(S)): if 'a'<=S[i]<=...原创 2020-09-30 22:12:53 · 97 阅读 · 0 评论 -
《Leetcode of September 》90. 子集 II
class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: def huisu(nums,path,index): if path not in res: print(path[:]) res.append(path[:]) ...原创 2020-09-30 21:23:27 · 135 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:78. 子集
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: def huisu(nums,path,visited,index): if path not in res: print(path[:]) res.append(path[:]) for ...原创 2020-09-30 20:35:58 · 157 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:79单词搜索
class Solution: def exist(self, board: List[List[str]], word: str) -> bool: m,n = len(board),len(board[0]) visited = [[False for i in range(n)] for i in range(m)] zuobiao = [[0,1],[0,-1],[1,0],[-1,0]] def huisu(...原创 2020-09-14 11:14:49 · 150 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:216. 组合总和 III
class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: def huisu(index,end,nums,temp,path): #剪枝 if path>n: return #和等于n而且个数还要是k if path==n and l...原创 2020-09-11 09:31:43 · 105 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:40. 组合总和 II
class Solution: def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: def huisu(candidates,index,n,path,temp): #剪枝操作 if path>target: return if path==t...原创 2020-09-10 15:30:11 · 143 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:39. 组合总和
class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: def huisu(candidates,nums,Sum): #如果Sum等于target,那就添加结果 if Sum==target: res.append(nums[:]) ...原创 2020-09-09 20:56:44 · 123 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:47. 全排列II
看题解看到一个比较形象的图,来自https://leetcode-cn.com/problems/permutations-ii/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liwe-2/class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: def huisu(n,temp,visited): ...原创 2020-09-08 20:50:15 · 264 阅读 · 0 评论 -
《Leetcode of September 》回溯算法之:46. 全排列
class Solution: def permute(self, nums: List[int]) -> List[List[int]]: def huisu(n,temp,visited): if len(temp)==n: res.append(temp[:]) return for i in range(n): ...原创 2020-09-08 17:27:44 · 83 阅读 · 0 评论 -
《Leetcode of September 》77. 组合
题目比较简单,使用回溯算法,之前已经记录过回溯算法的模板,在这个题目中需要注意的就是满足回溯出口条件的时候,给结果集中添加结果的时候应该是数组的shallow copy而不能直接添加数组。(至于为啥我还没搞清楚)直接添加的结果就是空数组。由于回溯算法的时间复杂度很高,因此,在遍历的时候,如果能够提前知道这一条分支不能搜索到满意的结果,这一分支就可以跳过,这一步操作就是在一棵树上剪去一个枝叶,被人们很形象地称之为剪枝。class Solution: def combine(self, n..原创 2020-09-08 17:05:58 · 103 阅读 · 0 评论 -
《Leetcode of September 》257.二叉树所有路径
采用两个方法去解决:1、DFS(其实就是回溯算法)2、BFS (相当于把所以节点显示的枚举出来)# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def binaryTre.原创 2020-09-04 15:25:12 · 143 阅读 · 0 评论 -
回溯算法之:N皇后的问题
题目链接:51.N皇后对8皇后问题其实早有耳闻,今天刚好看到这个题目,那就把这个题目搞明白。还是用了回溯算法,其实就是选择,试探,撤销。就是这三个步骤,首先回溯的结束条件要明确,那就是row==n了,就结束回溯。枚举所有列,然后选择,在进行递归,然后进行撤销操作。在枚举的这个循环下面,选择,递归,撤销这三个步骤是紧紧的挨着的。N皇后的问题就是然这N个皇后和平相处就行,所谓和平相处就是一行、一列、一条对角线只能有一个皇后。示例:输入:4输出:[ [".Q..", // 解法 1原创 2020-09-03 21:26:53 · 167 阅读 · 0 评论 -
回溯算法模板之:332. 重新安排行程
题目链接:332.重新安排行程分析:需要重新排列行程,并且需要按照自然排序的大小,所以第一步就先进行字典的创建,key是起始地,value是可到达所有的目的地,然后对value进行排序。使用回溯算法进行搜索,这下面有一个回溯的模板:回溯模板backtracking() { if (终止条件) { 存放结果; } for (枚举同一个位置的所有可能性,可以想成节点孩子的数量) { 递归,处理节点; backtracking原创 2020-08-27 16:45:14 · 185 阅读 · 0 评论