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 ...
随机推荐
- leetcode:94. 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? ...
- JavaScript中如何实现函数缓存?有哪些应用场景?
一.是什么 函数缓存,就是将函数运算过的结果进行缓存 本质上就是用空间(缓存存储)换时间(计算过程) 常用于缓存数据计算结果和缓存对象 const add = (a,b) => a+b;cons ...
- Sample HL7 ADT Messages
Here are a few sample ADT messages for testing that I've picked up over time. I may edit this post l ...
- 力扣387(java)-字符串中的第一个唯一字符(简单)
题目: 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 .如果不存在,则返回 -1 . 示例 1: 输入: s = "leetcode"输出: 0示例 2: 输 ...
- 力扣915(java&python)-分割数组(中等)
题目: 给定一个数组 nums ,将其划分为两个连续子数组 left 和 right, 使得: left 中的每个元素都小于或等于 right 中的每个元素.left 和 right 都是非空的.le ...
- 力扣605(java&python)-种花问题(简单)
题目: 假设有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给你一个整数数组 flowerbed 表示花坛,由若干 0 和 1 ...
- 牛客网-SQL专项训练14
①通过子查询的方式从衬衫表SHIRTABLE中选取出销售单价shirt_price高于全部衬衫的平均价格的衬衫名字:B 解析: 题目要求需要用到子查询,故排除AC选项,而D选项在where子句中使用了 ...
- 技术解读 | 智能开放搜索CTR预估模型
简介:本文介绍开放搜索CTR预估模型在个性化排序中的应用与实践 如何评价搜索排序效果? 搜索是用户触达信息最简单直接的方式,是APP.网页必备的功能.如何评价并提升搜索效果一直是搜索领域的常见问题. ...
- Spring RSocket:基于服务注册发现的 RSocket 负载均衡
简介: RSocket 作为通讯协议的后起之秀,核心是二进制异步化消息通讯,是否也能和 Spring Cloud 技术栈结合,实现服务注册发现.客户端负载均衡,从而更高效地实现面向服务的架构?这篇文章 ...
- 修复 WPF 安装 WindowsAppSDK 库构建失败 NETSDK1082 和 NETSDK1112 找不到 win10-arm 失败
通过在 WPF 项目上安装 WindowsAppSDK 库,可以让 WPF 使用上 Win10 及以上版本提供的 Windows Runtime 强大的 API 集和使用上更多的黑科技.本文记录在安装 ...