S212-搜索+字典树-212. Word Search II-(Hard)
一、题目

题目很简单,输入一个字母组成的二维数组,以某一个字母为起点,向、上下左右搜索、练成的字符看是不是在给定的字符串数组中
二、解答
通过深度优先搜索,每一步和数组中的字符串匹配是可以计算出来正确结果的,但是结果超时
重点是匹配的过程时间太长
通过字典树,可以优化两点
一是检索某个字符串在不在的时间
一是一个某个字符串前缀的字符串是否存在于字符串数组中
最终的答案:
注意,结果去重
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.x = {}
self.p = {} def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
if word:
self.x[word] = True
for i in range(1,len(word)+1):
self.p[word[0:i]] = True def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
""" for x in self.x:
if word == x:
return True
return False def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
return prefix in self.p class Solution:
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
:rtype: List[str]
"""
t = Trie()
for word in words:
t.insert(word) visited = [([0 for j in range(len(board[0]))]) for i in range(len(board))]
res = []
for row in range(len(board)):
for col in range(len(board[0])):
self.DFS(board, visited, "", row, col, t, res) return list(set(res)) def DFS(self, board, visited, temp_str, pos_row, pos_col, t, res):
if pos_row < 0 or pos_row >= len(board) or pos_col < 0 or pos_col >= len(board[0]):
return
# print(pos_row, pos_col, visited)
if visited[pos_row][pos_col] == 1:
return
temp_str += board[pos_row][pos_col]
if not t.startsWith(temp_str):
return
if t.search(temp_str):
res.append(temp_str) visited[pos_row][pos_col] = 1
self.DFS(board, visited, temp_str, pos_row, pos_col + 1, t, res)
self.DFS(board, visited, temp_str, pos_row - 1, pos_col, t, res)
self.DFS(board, visited, temp_str, pos_row, pos_col - 1, t, res)
self.DFS(board, visited, temp_str, pos_row + 1, pos_col, t, res)
visited[pos_row][pos_col] = 0 if __name__ == "__main__":
s = Solution()
# print(s.findWords([
# ['o','a','a','n'],
# ['e','t','a','e'],
# ['i','h','k','r'],
# ['i','f','l','v']
# ],
# ["oath","pea","eat","rain"])) print(s.findWords([["b"],["a"],["b"],["b"],["a"]],
["baa","abba","baab","aba"]))
三、参考
https://blog.csdn.net/ljiabin/article/details/45846527
S212-搜索+字典树-212. Word Search II-(Hard)的更多相关文章
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- [LeetCode] 212. Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- [leetcode trie]212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- 79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...
随机推荐
- nginx重新整理——————nginx 的设计模型[八]
前言 简单介绍一下nginx的设计模型,对我们设计程序还是有一定帮助的. 正文 这里先列一下模型哈,后面有深入篇,介绍的比较清楚. nginx 的处理模型: nginx 进程模型: 可以看到下面列出了 ...
- 面向切面编程AOP[三](java AnnotationAwareAspectJAutoProxyCreator实现了什么功能)
前言 要查看一个类实现了什么功能,那么查看它继承的接口或者class即可知道,那么其到底继承了什么? 正文 AnnotationAwareAspectJAutoProxyCreator extends ...
- Signalr断线重连机制
前言 Signalr 即时消息发布到服务器后发现链接老是自动断开,导致无法发送广播后面百度搜了一下,signalr有个超时的机制 解决办法(js) //链接到自己的hub var connection ...
- shell编程实现用户循环输入
如果你想在Shell脚本中实现一个循环判断用户输入是否正确,并根据情况决定是否退出系统,可以使用一个无限循环(如while true)和条件语句来实现. 以下是一个示例代码,用于演示这种情况: #!/ ...
- 力扣597(MySQL)-好友申请Ⅰ:总体通过率(简单)
题目: 此表没有主键,它可能包含重复项.该表包含发送请求的用户的 ID ,接受请求的用户的 ID 以及请求的日期. 此表没有主键,它可能包含重复项.该表包含发送请求的用户的 ID ,接受请求的用户的 ...
- OpenSergo 流量路由:从场景到标准化的探索
简介: 本文我们将从流量路由这个场景入手,从常见的微服务治理场景出发.先是根据流量路由的实践设计流量路由的 Spec,同时在 Spring Cloud Alibaba 中实践遵循 OpenSergo ...
- 智能数据构建与管理平台Dataphin的前世今生:缘起
简介: 阿里巴巴提出的OneData方法论帮助企业捋清了数据全生命周期的管理思路,更将其植入到产品Dataphin(智能数据构建与管理)中,通过阿里云为企业提供服务. Dataphin 智能数据构建与 ...
- Total Commander 使用 mklink 建立文件夹链接 将 C 盘文件迁移到其他盘
在安装完成了 100000000 个软件之后,我 1T 的 C 盘的空间终于不足了,由于安装了大量的特别挑的不专业的软件,强行放在其他的盘将水土不服.于是在老师傅的指导下,我采用了 mklink 神奇 ...
- Part-DB 配置流程
介绍 Part-DB是一个开源的器件管理工具,博主用于管理个人的电子器材,最近捣鼓了一下这个工具,由于手头还有一块闲置的赛昉·星光2的开发板,所以我打算一起拿来捣鼓一下,如果不成功,就用树莓派(生气) ...
- ES_CCS/R(一):建立集群之间的安全互信
为了能够实现 CCR(跨集群复制 Cross-cluster replication) 及 CCS(跨集群搜索 Cross-cluster search ),我们必须让集群之间能够互信,这样才可以建立 ...