【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 ...
随机推荐
- Go使用详解
1.什么是Go keep it simple stupid的编程语言 2.安装 以Ubuntu为例 # 下载安装包 wget https://storage.googleapis.com/golang ...
- Asp.Net MVC三层架构之autofac使用教程
开发环境:vs2015..net4.5.2.mvc5.ef6 Autofac简介 IOC控制反转(Inversion of Control,缩写为IOC),Autofac是一个开源的依赖注入框架,Au ...
- jQuery雷达扫描切换幻灯片代码
基于jQuery雷达扫描切换幻灯片代码.这是一款切换效果类似雷达扫描,支持鼠标滚轮滚动切换.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class=" ...
- WPF ViewModelLocator
在WPF中应用ViewModelLocator <Window x:Class="Demo.Views.MainWindow" ... xmlns:prism="h ...
- 【Unity】微信支付SDK官方安卓Demo的使用问题
Unity3d使用微信支付是属于APP内发起支付调用的情况,其本质上是在安卓项目上使用微信SDK,安卓项目开发完成后再导入到Unity中作为Unity插件使用,即Unity中C#调用安卓(Java)代 ...
- 【静默】Oracle各类响应文件何在?
[静默]Oracle各类响应文件何在? --root用户下执行: find -name *.rsp / 1.创建数据库的响应文件:$ORACLE_HOME/assistants/dbca/dbca. ...
- Mysql Window 解压版卸载
windows如何彻底卸载mysql 如何彻底删除mysql 1.首先在windows服务中将mysql服务删掉,使用命令 sc delete mysql 2.在控制面板中卸载掉mysql. 3.清理 ...
- linux 使用不安全的sprintf函数,存储字符越界导致程序莫名崩溃问题
linux c++编程 问题背景: 在处理一个公共模块的代码中,其中有以下代码片段 //代码片段-组合一组字符串并存放到szSignKey数组中 ] = {}; sprintf(szSignKey, ...
- linux查找并替换命令
find ./ -maxdepth 3 -type f -name "*Makefile" |xargs sed -i "s/CXX = g++/CXX = ccac ...
- Phoenix 5.0 hbase 2.0 org.apache.hadoop.security.authentication.util.KerberosUtil.hasKerberosKeyTab
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...