龙岗义乌网站制作,php做网站python做什么,零遁nas做网站,好网站分享LeetCode-79. 单词搜索【数组 字符串 回溯 矩阵】 题目描述#xff1a;解题思路一#xff1a;回溯 回溯三部曲。这里比较关键的是给board做标记#xff0c;防止之后搜索时重复访问。解题思路二#xff1a;回溯算法 dfs,直接看代码,很容易理解。visited哈希#xff0c;防止… LeetCode-79. 单词搜索【数组 字符串 回溯 矩阵】 题目描述解题思路一回溯 回溯三部曲。这里比较关键的是给board做标记防止之后搜索时重复访问。解题思路二回溯算法 dfs,直接看代码,很容易理解。visited哈希防止重复访问。解题思路三0 题目描述
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中返回 true 否则返回 false 。
单词必须按照字母顺序通过相邻的单元格内的字母构成其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例 1 输入board [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word “ABCCED” 输出true 示例 2 输入board [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word “SEE” 输出true 示例 3 输入board [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word “ABCB” 输出false
提示
m board.length n board[i].length 1 m, n 6 1 word.length 15 board 和 word 仅由大小写英文字母组成
进阶你可以使用搜索剪枝的技术来优化解决方案使其在 board 更大的情况下可以更快解决问题
解题思路一回溯 回溯三部曲。这里比较关键的是给board做标记防止之后搜索时重复访问。 递归函数参数 这里的参数是 当前元素在矩阵 board 中的行列索引 i 和 j 当前目标字符在 word 中的索引 index 。 递归终止条件 返回 false (1) 行或列索引越界 或 (2) 当前矩阵元素与目标字符不同 或 (3) 当前矩阵元素已访问过 (3) 可合并至 (2) 。 返回 true k len(word) - 1 即字符串 word 已全部匹配。 单层搜索的逻辑 标记当前矩阵元素 将 board[i][j] 修改为 空字符 ‘’ 代表此元素已访问过防止之后搜索时重复访问。 搜索下一单元格 朝当前元素的 上、下、左、右 四个方向开启下层递归使用 或 连接 代表只需找到一条可行路径就直接返回不再做后续 DFS 并记录结果至 res 。 还原当前矩阵元素 将 board[i][j] 元素还原至初始值即 word[k] 。 返回值 返回布尔量 res 代表是否搜索到目标字符串。
使用空字符Python: ‘’ , Java/C: ‘\0’ 做标记是为了防止标记字符与矩阵原有字符重复。当存在重复时此算法会将矩阵原有字符认作标记字符从而出现错误。
class Solution:def __init__(self):self.dirs [(-1, 0), (0, 1), (1, 0), (0, -1)]def exist(self, board: List[List[str]], word: str) - bool:m, n len(board), len(board[0])for i in range(m):for j in range(n):if self.backtracking(board, i, j, 0, word):return Truereturn Falsedef backtracking(self, board, x, y, index, word):if x 0 or x len(board) or y 0 or y len(board[0]) or board[x][y] ! word[index]:return Falseif index len(word) - 1:return Trueres Falsefor d in self.dirs:nextx x d[0]nexty y d[1]board[x][y] res self.backtracking(board, nextx, nexty, index1, word) or resboard[x][y] word[index]return res在代码中M,N 分别为矩阵行列大小 K 为字符串 word 长度。
时间复杂度 O(3KMN) 最差情况下需要遍历矩阵中长度为 K 字符串的所有方案时间复杂度为 O(3K)矩阵中共有 MN 个起点时间复杂度为 O(MN) 。 方案数计算 设字符串长度为 K 搜索中每个字符有上、下、左、右四个方向可以选择舍弃回头上个字符的方向剩下 333 种选择因此方案数的复杂度为 O(3K)。 空间复杂度 O(K) 搜索过程中的递归深度不超过 K 因此系统因函数调用累计使用的栈空间占用 O(K) 因为函数返回后系统调用的栈空间会释放。最坏情况下 KMN递归深度为 MN 此时系统栈使用 O(MN) 的额外空间。
解题思路二回溯算法 dfs,直接看代码,很容易理解。visited哈希防止重复访问。
class Solution:def exist(self, board: List[List[str]], word: str) - bool:row len(board)col len(board[0])def helper(i, j, k, visited):#print(i,j, k,visited)if k len(word):return Truefor x, y in [(-1, 0), (1, 0), (0, 1), (0, -1)]:tmp_i x itmp_j y jif 0 tmp_i row and 0 tmp_j col and (tmp_i, tmp_j) not in visited \and board[tmp_i][tmp_j] word[k]:visited.add((tmp_i, tmp_j))if helper(tmp_i, tmp_j, k1, visited):return Truevisited.remove((tmp_i, tmp_j)) # 回溯return Falsefor i in range(row):for j in range(col):if board[i][j] word[0] and helper(i, j, 1,{(i, j)}) :return Truereturn False时间复杂度O(3KMN) 空间复杂度O(K)
解题思路三0 时间复杂度O(n) 空间复杂度O(n) 文章转载自: http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.mqfkd.cn.gov.cn.mqfkd.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.btqrz.cn.gov.cn.btqrz.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.jpwkn.cn.gov.cn.jpwkn.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn