【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 ...
随机推荐
- iOS开发下载文件速度计算
当我们写下载界面的时候,需要向用户展示每秒下载多少KB,这个时候就需要计算速度.如下: 我用的是AFNetworking来做下载的,我们拿AFHTTPRequestOperation来举列,AFHTT ...
- iOS最新Mac OS X 10.11之后 安装cocoapods及使用详解
iOS 最新版 CocoaPods 的安装流程 一.安装方法: 1.移除现有Ruby默认源 gem sources --remove https://rubygems.org/ 为了提高安装的成功几率 ...
- C语言 · 猜算式
题目:猜算式 看下面的算式: □□ x □□ = □□ x □□□ 它表示:两个两位数相乘等于一个两位数乘以一个三位数. 如果没有限定条件,这样的例子很多. 但目前的限定是:这9个方块,表示1~9的9 ...
- Protocol Buffers学习教程
最近看公司代码的过程中,看到了很多proto后缀的文件,这是个啥玩意?问了大佬,原来这是Protocol Buffers! 这玩意是干啥的?查完资料才知道,又是谷歌大佬推的开源组件,这玩意完全可以取代 ...
- WebSphere MQ中的CCSID
CCSID是一个字符集的标识.作为unicode标准通过定义一个字符集内每个字符要对应那个数字值的方式定义了一个字符集.这说明CCSID就是一个定义字符集顺序的标识数码罢了.IBM的字符标识架构在文档 ...
- springboot-multisource
项目中经常会出现需要同时连接两个数据源的情况,这里基于MyBatis来配置两个数据源,并演示如何切换不同的数据源. 通过自定义注解+AOP的方式,来简化这种数据源的切换操作. <properti ...
- Webservice学习之WSDL详解
1. <definitions/> 这部分在基础篇里已经介绍,主要说明引用了哪些schema以及schema的位置等,可以看下基础篇的介绍,SayHello的Demo这部分内容如下: &l ...
- [原创]WB Android客户端架构总结:发WB工作队列设计
先简单说下需求,发一条WB包含多种类型,例如图片.视频.文字等,发送工作不能阻塞UI,工作队列易于扩展,方便优化. 几个重要的类: JobManager:统一管理Job列表,包括job的添加.启动.终 ...
- Ubuntu系统的nginx启动
在不同的linux系统中,安装nginx之后,要启动nginx,目录路径可能有一点不一样,如下是Ubuntu系统启动nginx,其他版本的linux系统可能不适用. Ubuntu安装之后的文件结构大致 ...
- cf 1132 F
区间dp.. 每次删一串相邻的一样的,问多少次删光. 感觉想的几乎是一样的怎么比赛时就过不了呢...一定是因为我挂机睡觉了 考虑l和r相等,l和l+1相等,r和r-1相等这三种情况就行了..然后就是裸 ...