一、题目

  

   题目很简单,输入一个字母组成的二维数组,以某一个字母为起点,向、上下左右搜索、练成的字符看是不是在给定的字符串数组中

二、解答

  通过深度优先搜索,每一步和数组中的字符串匹配是可以计算出来正确结果的,但是结果超时

  重点是匹配的过程时间太长

  通过字典树,可以优化两点

  一是检索某个字符串在不在的时间

  一是一个某个字符串前缀的字符串是否存在于字符串数组中

  最终的答案:

  注意,结果去重

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)的更多相关文章

  1. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  2. [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 ...

  3. [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 ...

  4. 【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 ...

  5. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  6. [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 ...

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

  8. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. HarmonyOS 性能优化

    如何合理使用动效来获得更好的性能 组件转场动画使用 transition: 推荐使用转场动画(transition)而不是组件动画(animateTo),因为 transition 只需要在条件改变时 ...

  2. React中类组件和函数组件

    一.类组件 类组件,顾名思义,也就是通过使用ES6类的编写形式去编写组件,该类必须继承React.Component 如果想要访问父组件传递过来的参数,可通过this.props的方式去访问 在组件中 ...

  3. JavaScript中字符串小知识

    1. 字符串是不可变的 字符串一旦创建就是不可变的,后续的修改都是新建一个新的字符串而不是在原有的字符串上修改 // 在内存中开辟 可以存放五个字母的空间 str指向该空间 let str = 'fi ...

  4. Spring 源码阅读(二)IoC 容器初始化以及 BeanFactory 创建和 BeanDefinition 加载过程

    相关代码提交记录:https://github.com/linweiwang/spring-framework-5.3.33 IoC 容器三种启动方式 XML JavaSE: ApplicationC ...

  5. 传统 Web 框架部署与迁移

    简介: 与其说 Serverless 架构是一个新的概念,不如说它是一种全新的思路,一种新的编程范式. 与其说 Serverless 架构是一个新的概念,不如说它是一种全新的思路,一种新的编程范式. ...

  6. 如何用一个插件解决 Serverless 灰度发布难题?

    简介: 我们可以发现相比使用控制台进行灰度发布,使用 FC-Canary 插件免去了用户手动创建版本.发布别名.关联触发器和管理自定义域名的麻烦,使用起来非常方便. 作者:长淇 导读 本文适合: 想了 ...

  7. Scheduled SQL: SLS 大规模日志上的全局分析与调度

    简介: 本文总结了大规模日志全局分析的需求,讨论SLS上现有的典型分析方案,并延伸到 SLS 原生数据处理方案,介绍 Schedueld SQL 功能与最佳实践. 大规模日志全局分析的需求 数据大规模 ...

  8. DataWorks 功能实践速览

    ​简介: DataWorks功能实践系列,帮助您解析业务实现过程中的痛点,提高业务功能使用效率! 功能推荐:独享数据集成资源组 如上期数据同步解决方案介绍,数据集成的批数据同步任务运行时,需要占用一定 ...

  9. dotnet 6 HttpClientHandler 和 SocketsHttpHandler 有什么差别

    本文来告诉大家在 dotnet 6 的 HttpClientHandler 和 SocketsHttpHandler 两个类型有什么不同 在创建 HttpClient 时,可以在 HttpClient ...

  10. STM32F10X 部分引脚不能使用的问题

    一.概述 说来惭愧,我到现在都没有完整的学习过STM32.接触 STM32 还是突然的一个项目,需要用到 STM32,紧急需求,只能边学边完成.不过好在 ST 的资料还是比较多的,相对也比较简单,基本 ...