![](https://img-blog.csdnimg.cn/2019092715111047.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
LC回溯
LC回溯算法的专栏
996冲冲冲
这个作者很懒,什么都没留下…
展开
-
回溯算法总结
当我们回溯路径是用list列表记录的时候,每次调用方法都会改变列表的值(因为python列表是可变数据类型)所以想回复上一状态需要撤销操作。当我们使用的是字符串类型时,需要弄清楚方法调用的顺序和每个方法里变量的值。虽然说字符串类型是不可变的数据类型,即每次调用方法都会创建一个新的值。但是由于调用的顺序不一样,每个方法里的变量可能和想象的不一样,所以需要自己注意,有时也需要有撤销操作。...原创 2022-03-16 16:37:17 · 451 阅读 · 0 评论 -
leetcode967. 连续差相同的数字
从第一个数字(可能是一到九)开始,然后每次以最后一个进行改变搜索(+k,-k)。原创 2023-10-15 22:52:18 · 175 阅读 · 0 评论 -
LC332. 重新安排行程
注意defaultdict是一个很有用的方法,注意熟练的使用def findItinerary(self, tickets): """ :type tickets: List[List[str]] :rtype: List[str] """ mp = collections.defaultdict(list) for x,y in tickets: #建立邻接表原创 2022-05-05 14:50:19 · 185 阅读 · 0 评论 -
LC797. 所有可能的路径
BFSdef allPathsSourceTarget(self, graph): """ :type graph: List[List[int]] :rtype: List[List[int]] """ queue = deque() queue.append([0]) res,tmpath = [],[] while queue: #原创 2022-04-29 17:11:55 · 382 阅读 · 0 评论 -
LC329. 矩阵中的最长递增路径
回溯加记忆,记忆的原因是在for循环里回溯时会遇到已经计算过的值,避免重复计算def longestIncreasingPath(self, matrix): """ :type matrix: List[List[int]] :rtype: int """ if not matrix: return 0 save = [[0 for i in range(len(matrix[0]))原创 2022-04-23 21:06:04 · 330 阅读 · 0 评论 -
LC301. 删除无效的括号
DFS+剪枝def removeInvalidParentheses(self, s): """ :type s: str :rtype: List[str] """ l = r = 0 for c in s: if c == '(': l += 1 elif c == ')': if l:原创 2022-04-21 21:14:09 · 105 阅读 · 0 评论 -
LC733. 图像渲染
BFSdef floodFill(self, image, sr, sc, newColor): """ :type image: List[List[int]] :type sr: int :type sc: int :type newColor: int :rtype: List[List[int]] """ cur = image[sr][sc] if cu原创 2022-03-12 22:05:34 · 108 阅读 · 0 评论 -
LC695. 岛屿的最大面积
DFSclass Solution(object): def maxAreaOfIsland(self,grid): row, col = len(grid), len(grid[0]) def backtrack(x, y): grid[x][y] = 0 count = 1 for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y -原创 2022-03-14 20:24:28 · 312 阅读 · 0 评论 -
LC463. 岛屿的周长
DFSclass Solution(object): def islandPerimeter(self,grid): count = 0 def backtrack(count, x, y): grid[x][y] = 2 for nx, ny in [(x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1)]: if not(0<=nx<原创 2022-03-14 17:41:29 · 319 阅读 · 0 评论 -
LC257. 二叉树的所有路径
class Solution(object): def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ res,sol = [],"" def backtrack(root,sol): if root: sol += str(root.val)原创 2022-03-16 17:04:52 · 926 阅读 · 0 评论 -
LC216. 组合总和 III
class Solution(object): def combinationSum3(self, k, n): """ :type k: int :type n: int :rtype: List[List[int]] """ res,sol = [],[] if n > 45 or n < 0 or k < 0: return sol原创 2022-03-16 10:02:33 · 99 阅读 · 0 评论 -
LC212. 单词搜索 II
思路简单纯暴力,会卡最后一个用例。class Solution(object): def findWords(self,board, words): sol, res = [], set() row = len(board) col = len(board[0]) mark = [[0 for _ in range(col)] for _ in range(row)] board_count = collections原创 2022-03-19 18:14:12 · 116 阅读 · 0 评论 -
LC200. 岛屿数量
DFS不修改原数组class Solution(object): def numIslands(self, grid): """ :type grid: List[List[str]] :rtype: int """ count = 0 row,col = len(grid),len(grid[0]) vsd = [[0 for i in range(col)] for j in ran原创 2022-03-14 11:09:59 · 546 阅读 · 0 评论 -
LC. 131. 分割回文串
动态规划保存是否是回文串结果法class Solution(object): def partition(self, s): """ :type s: str :rtype: List[List[str]] """ res, sol = [],[] n = len(s) f = [[True]*n for i in range(n)] for i in range(n-1原创 2022-03-11 16:22:16 · 6258 阅读 · 0 评论 -
LC93. 复原 IP 地址
def restoreIpAddresses(self, s): """ :type s: str :rtype: List[str] """ res,sol = [],[] def help(tmp): if tmp == "0": return 1 if tmp[0]== "0": return 0 if int(tmp) >原创 2022-03-11 22:18:46 · 75 阅读 · 0 评论 -
LC90. 子集 II
同层剪枝法class Solution(object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() res = [] sol = [] def backtrack(res, sol, begin):原创 2021-11-14 20:24:58 · 209 阅读 · 0 评论 -
LC89. 格雷编码
DFS暴力def grayCode(self,n): res = [0] l = pow(2, n) still = {i for i in range(1, l)} def backtrack(still, n): if len(res) == l: return pre = res[-1] for i in range(n):原创 2022-03-18 21:09:12 · 219 阅读 · 0 评论 -
LC79. 单词搜索
回溯,注意返回什么def exist(self,board, word): row = len(board) col = len(board[0]) mark = [[0 for _ in range(col)] for _ in range(row)] def backtrack(r, c, begin): if board[r][c] != word[begin]: return F原创 2022-03-19 14:47:25 · 151 阅读 · 0 评论 -
LC78. 子集
class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] sol = [] def backtrack(res, sol, begin): res.append(sol[:])原创 2021-11-14 21:30:09 · 354 阅读 · 0 评论 -
LC77. 组合
class Solution(object): def combine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] """ res = [] sol = [] if k < 0 or n < k: return res def b..原创 2021-11-14 20:06:15 · 325 阅读 · 0 评论 -
LC60. 排列序列
count = ji[n - 1 - index]这一句首先是选一个的时候的排列个数,之所以这样写因为第一轮也就是比如n=4, k=15选1之后k=k-6. 选2之后 k= k -6 -6 。 在同一层每次减去的选择个数相同,因为剩余的数相同。比如n=4时做第一个选择之后剩余的数都是3也就是都剩下6种排列if count < k:k -= countcontinue这三句表示在同一层时每次的count也就是剩下的选择的个数相同continue结束本次循环index控制层数,index原创 2021-11-14 22:40:40 · 309 阅读 · 0 评论 -
LC52. N皇后 II
def totalNQueens(self, n): """ :type n: int :rtype: int """ matrix = [['.'] * n for _ in range(n)] res = [] self.count = 0 def help(matrix,r,c,n): for i in range(r):原创 2022-03-16 21:41:43 · 105 阅读 · 0 评论 -
LC51. N 皇后
此处需要注意的是因为答案其实是在矩阵里,回溯也是回溯的矩阵。所以在append的时候,每次要把前一个sol给置为空。class Solution(object): def solveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ matrix = [['.'] * n for _ in range(n)] res = []原创 2022-03-16 21:39:29 · 355 阅读 · 0 评论 -
LC 47重复全排列
class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() res = [] vsd = [0]*len(nums) sol = [] def backtrack(nums,..原创 2021-11-09 21:15:41 · 4645 阅读 · 0 评论 -
LC46. 全排列
```pythonclass Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ res = [] sol = [] vsd = [0]*len(nums) def backtrack(nums,sol,vsd): .原创 2021-10-29 15:36:25 · 105 阅读 · 0 评论 -
LC40. 组合总和 II
没声明candidates数组不重复,所以去重时参考全排列二,对于数组里的重复元素需要进行去重.相比于组数总和一这题需要两次去重,第一次是深度优先遍历里同一支不可以重复使用就是i+1. 第二次是因为有相同的元素,所以前面用过的再后面的分支里也不能再使用。注意使用了used数组之后还是需要begin因为顺序不一样的也算是重复。class Solution(object): def combinationSum2(self, candidates, target): """原创 2021-11-10 22:25:29 · 4060 阅读 · 0 评论 -
LC39. 组合总和
组数总和问题,此类问题设置递归的begin就可以实现去重的功能。原因在于先对较小数使用得到的答案必然包含了后面先用较大的数再用较小的数这种组合方式。class Solution(object): def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]]原创 2021-11-10 22:03:01 · 305 阅读 · 0 评论 -
LC37. 解数独
求可用数字之后三个条件的交集解法def solveSudoku(self, board): """ :type board: List[List[str]] :rtype: None Do not return anything, modify board in-place instead. """ row = [set(range(1,10)) for _ in range(9)] col = [set(ra原创 2022-03-17 21:25:05 · 146 阅读 · 0 评论 -
LC22. 括号生成
这里backtrack只传了参数,所以当前方法backtrack执行之后才会改变sol的值所以不用回溯class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ res = [] sol = "" def backtrack(sol,left,right,原创 2022-03-16 09:29:33 · 274 阅读 · 0 评论 -
LC17. 电话号码的字母组合
class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ res = [] s = "" map = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"] if原创 2022-03-12 13:46:28 · 105 阅读 · 0 评论