【LeetCode每天一题】Word Search(搜索单词)
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(搜索单词)的更多相关文章
- 【python】Leetcode每日一题-二叉搜索迭代器
[python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...
- 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 ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- 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 ...
- 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 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 ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
随机推荐
- firewall防火墙使用
firewall用zone来区分管理,默认有以下一些zone: 丢弃(DROP) 任何流入网络的包都被丢弃,不作出任何响应.只允许流出的网络连接. 阻塞(Block) 任何进入的网络连接都被拒绝,并返 ...
- C# Task中的Func, Action, Async与Await的使用
在说Asnc和Await之前,先说明一下Func和Action委托, Task任务的基础的用法 1. Func Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate, ...
- 如何知道局域网内哪些ip被占用----工具法Free IP Scanner
在局域网中,尤其是在工作室和公司中需要修改IP地址才能上网,通常我们在设置完ip地址后会提示[该ip地址已被占用],又得回头去修改ip地址.本篇经验就介绍一款很好用的免费软件——Free IP Sca ...
- 【iCore1S 双核心板_ARM】例程十八:SD_IAP_FPGA实验——更新升级FPGA
实验现象及操作说明: 1.烧写程序成功,绿色ARM·LED灯点亮,三色FPGA·LED灯循环点亮,烧写失败,如果挂载SD卡失败,红灯快闪,如果打开文件失败,蓝灯快闪,读取文件指针移动失败,白灯点亮,升 ...
- [Z] SQL SERVER 的前世今生--各版本功能对比
https://www.cnblogs.com/OwenZeng/p/6813143.html
- JavaScript数组删除指定元素
^_^ function arrayRemoveItem(arr, delVal) { if (arr instanceof Array) { var index = arr.indexOf(delV ...
- VMware ESXI添加第三方网卡驱动
VMware ESXI有两种方法添加第三方网卡驱动: 1.使用第三方工具 ESXI-Customizer.cmd工具可以将已经下载好的VMware ESXI.ISO镜像文件把下载好的驱动添加到里面,缺 ...
- web worker 简介
web worker 简介 通常,浏览器执行某段程序的时候会阻塞直到运行结束后在恢复到正常状态,而HTML5的Web Worker就是为了解决这个问题.通过worker线程完成密集计算,避免程序的阻塞 ...
- 运用kmp算法解决的一些问题的简单题解
学习kmp算法我最后是看的数据结构书上的一本教材学会的..我认为kmp相对于普通的BF算法就是避免了非常多不必要的匹配.而kmp算法的精髓自然就在于next数组的运用...而next数组简而言之就是存 ...
- linux shell的here document用法(cat << EOF)
什么是Here Document?Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下cmd << delimiter Here Docu ...