leetcod79题在矩阵中寻找某个单词

"""
79. Word Search
MediumShare
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.Example:board =
[['A','B','C','E'],['S','F','C','S'],['A','D','E','E']
]Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
"""

用深度优先搜索,首先找到首单词,因为矩阵中的单个元素只能用一次所以要记录路径,还要记录当前位置,

class Solution(object):def searchWordinBoard(self, board, word, len_word, n, i, j, pathset):""":type board:list[list[str]]:type word:str:type n,i,j:int:type pathset:set:rtype:bool几个参数分别代表矩阵,单词,单词长度,当前字母,当前矩阵位置,已经经过的路径集合"""if i<0 or j<0 or i>=len(board) or j>=len(board[0]):return Falseif word[n]!=board[i][j] or (i,j) in pathset:return Falseif n == len_word-1:return Truepathset.add((i,j))result = self.searchWordinBoard(board, word, len_word, n+1, i-1, j, pathset) or \self.searchWordinBoard(board, word, len_word, n+1, i, j-1, pathset) or \self.searchWordinBoard(board, word, len_word, n+1, i+1, j, pathset) or \self.searchWordinBoard(board, word, len_word, n+1, i, j+1, pathset)pathset.remove((i,j))return resultdef exist(self, board, word):""":type board: List[List[str]]:type word: str:rtype: bool"""if word == "":return Trueif not board or not board[0]:return Falselen_row, len_col = len(board), len(board[0])for i in range(len_row):for j in range(len_col):if word[0] != board[i][j]:continueif self.searchWordinBoard(board, word, len(word), 0, i, j, set()):return Truereturn False

转载于:https://www.cnblogs.com/mangmangbiluo/p/10359894.html

79. Word Search相关推荐

  1. 【DFS + Backtracking】LeetCode 79. Word Search

    LeetCode 79. Word Search Solution1:我的答案 DFS + Backtracking class Solution { public:bool exist(vector ...

  2. LeetCode 79. Word Search

    原题链接在这里:https://leetcode.com/problems/word-search/ 题目: Given a 2D board and a word, find if the word ...

  3. 79. Word Search 单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  4. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  5. leetcode 79. Word Search | 79. 单词搜索(回溯+DFS)

    题目 https://leetcode.com/problems/word-search/ 题解 回溯 + DFS,回溯是有后效性的,所以不能转化为 DP class Solution {int M; ...

  6. 【CODE】Unique Paths Word Search (DFS dp 字典树)

    目录 62. Unique Paths 63. Unique Paths II 980. Unique Paths III 79. Word Search 212. Word Search II 字典 ...

  7. 【DFS + Backtracking】LeetCode 212. Word Search II

    LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...

  8. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  9. 【Python3】 LeetCode 7月挑战题目之21 - Word Search

    目录/Table of Content 第二十一天问:Word Search 题目&示例 (引用自 LeetCode) 解题思路 失败代码 (不完整,仅供自己存档) 参考代码 (引用自CSDN ...

最新文章

  1. NeHe教程Qt实现——lesson11
  2. 讲解泊松融合文章推荐
  3. 日志库EasyLogging++学习系列(3)—— 配置功能
  4. 怎样在Linux系统中编译,Linux操作系统如何编译程序?
  5. while用法_语法||由一句译文聊聊while的用法
  6. 基于深度学习的磁环表面缺陷检测算法
  7. undo_management设置与隐含参数*._offline_rollback_segments和*._corrupted_rollback_segments关系...
  8. android打开相机拍照及打开相册选择照片
  9. 优化算法进阶;word2vec;词嵌入进阶
  10. AVOD-代码理解系列(四)
  11. 超像素分割研究进展+SLIC近几年进展
  12. phabricator 结合 arcanist 使用
  13. Soft Filter Pruning (SFP)——允许更新Pruned Filters的Channel Pruning策略
  14. IDEA中springboot项目@SpringBootApplication飘红问题的奇葩解决方案
  15. 实验吧CTF逆向题1000writeup
  16. 计算机软件工程考研考哪些专业,软件工程考研考什么科目?
  17. 网络钓鱼攻击类型,载体及其技术途径
  18. 金蝶kis记账王凭证过账要不要要审核
  19. 关于QT跨平台和编译器的理解
  20. 安卓rk3288软件的烧写过程

热门文章

  1. Extjs鼠标长按事件(实现长按按钮触发事件的方法:mousedown、mouseup)
  2. RFC 协议下载方法
  3. ajax校验用户名可用吗,基于jQuery实现Ajax验证用户名是否可用实例
  4. double java 坑,Java中四则运算的那些坑
  5. java循环使用范围_Java循环流程控制语句
  6. w7系统计算机网络密码是什么,解答win7系统共享弹出Windows安全窗口提示输入网络密码的详细教程...
  7. linux18.04安装显卡驱动,详细介绍ubuntu18.04安装NVIDIA显卡驱动(亲测有效!)
  8. html禁止f12键代码,网站禁用f12 禁止调试代码方法
  9. java命名规则_Java命名规则
  10. windows端自动化遇到的问题