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.
思路

  在二维矩阵中搜索单词,首先在矩阵中找到word中第一个字符的位置,然后判断该位置是否可以找到word中所有字符,如果没有找到我们继续在矩阵中遍历直到找到下一个与word中首字母相同的单词然后继续判断。如果最后矩阵遍历完毕之后还是没找到,说明矩阵中不存在word。直接返回False。 另外在矩阵中搜寻word单词剩余的部分时,我们需要设置一个辅助矩阵用来记录该位置是否已经搜索过了。
解决代码


 class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
row, cloum = len(board), len(board[0])
tem = [] # 设置辅助矩阵
for i in range(row):
tem.append([0]*cloum) for i in range(row): # 开始遍历查找
for j in range(cloum):
if board[i][j] == word[0]: # 扎到矩阵中与word首字母相等的位置
res = self.find_res(board, word, i, j, 0, tem)
if res:
return res
return False def find_res(self, board, word, row, cloum, index, tem):
if index >= len(word): # 如果index 大于word长度,说明已经遍历完毕,在矩阵中能找到word
return True
if row >= len(board) or cloum >= len(board[0]) or row <0 or cloum < 0 or tem[row][cloum] == 1: # 异常情况
return False tem[row][cloum] = 1
if board[row][cloum] == word[index]: # 四种走法, 上下左右方向都需要判断,中间使用or表示只要有一条路径为True,则结果为True
res = self.find_res(board, word, row+1, cloum, index+1, tem) | self.find_res(board, word, row, cloum+1, index+1, tem) | self.find_res(board, word, row-1, cloum, index+1, tem) | self.find_res(board, word, row, cloum-1, index+1, tem)
if res == True: # 直接返回结果
return True
tem[row][cloum] =0 # 说明没找到,将位置设置会初始状态
return False

【LeetCode每天一题】Word Search(搜索单词)的更多相关文章

  1. 【python】Leetcode每日一题-二叉搜索迭代器

    [python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...

  2. LeetCode 79. Word Search(单词搜索)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

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

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

  4. LeetCode OJ:Word Search(单词查找)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...

  6. 【LeetCode每天一题】Search Insert Position(搜索查找位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. LeetCode(79) Word Search

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  8. [LeetCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. [LeetCode] Stickers to Spell Word 贴片拼单词

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

随机推荐

  1. input文件上传(上传单个文件/多选文件/文件夹、拖拽上传、分片上传)

    //上传单个/多个文件 <input title="点击选择文件" id="h5Input1" multiple="" accept= ...

  2. HTTP 03 HTTP 报文

    客户端的HTTP报文, 叫做请求报文 服务器端的叫做 响应报文. HTTP 报文本身是由多行 (用 CR+LF 作换行符) 数据构成的字符串文本. HTTP 报文大致分为报文首部 和 报文主体 两部分 ...

  3. vs2017离线安装且安装包不占用C盘空间

    [参考]vs2017离线安装且安装包不占用C盘空间 第一步:下载离线安装包 https://www.visualstudio.com/zh-hans/downloads/ 在官方地址下载vs_prof ...

  4. spring JdbcTemplate数据库

    DAO package DAO; import org.springframework.jdbc.core.JdbcTemplate; public class OrderDao { private ...

  5. SpringBoot------连接MySQL报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized

    报错提示: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zon ...

  6. Java 8 时间日期

    啦啦啦 package lime.java1_8.time; import java.time.*; import java.time.format.DateTimeFormatter; import ...

  7. 将ubuntu的home迁移至第二块磁盘

    在忍受了一整周的磁盘将满的报警之后,今天终于着手准备将占据64G磁盘中的44G的Home迁移至另外一块磁盘,当然,这也是使用Linux做PC OS的正确使用方式.在Linux的目录管理风格的基础上,这 ...

  8. thinkcmf 5关闭后台验证码

    控制器修改 D:\cmf\thinkcmf\app\admin\controller\PublicController.php 注释 /* $captcha = $this->request-& ...

  9. Webservice学习之WSDL详解

    1. <definitions/> 这部分在基础篇里已经介绍,主要说明引用了哪些schema以及schema的位置等,可以看下基础篇的介绍,SayHello的Demo这部分内容如下: &l ...

  10. jenkins之 Throttle Concurrent Builds使用

    Jenkins控制并发插件 Throttle Concurrent Builds介绍,管网见:https://github.com/jenkinsci/throttle-concurrent-buil ...