一、题目

  

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

二、解答

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

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

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

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

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

  最终的答案:

  注意,结果去重

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. mysql 重新整理——索引优化explain字段介绍一 [九]

    前言 在七种介绍了explain这东西,那么具体来看下它是如何来运行的吧. 正文 id 来看一条语句:EXPLAIN select * from departments,dept_emp,employ ...

  2. node require 运行步骤

    前言 准备整理node 系列,先把一些基础含义放出来. 在学习node 的时候我们一般加载模块都是require,那么require 是如何运行的呢? 正文 通常,在Node.js里导入是通过 req ...

  3. c# 后端与前端时间戳的转换

    C# DateTime与时间戳转换 C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的 ...

  4. 【.net】Error while trying to retrieve text for error ORA-06413

    [.net]Error while trying to retrieve text for error ORA-06413 系统需要,有一个外挂系统使用的是.net,出了问题,所以部署环境查询代码 但 ...

  5. 力扣618(MySQL)-学生地理信息报告(困难)

    题目: 一所美国大学有来自亚洲.欧洲和美洲的学生,他们的地理信息存放在如下 student 表中 该表没有主键.它可能包含重复的行.该表的每一行表示学生的名字和他们来自的大陆. 一所学校有来自亚洲.欧 ...

  6. 力扣1337(java&python)-矩阵中战斗力最弱的 K 行(简单)

    题目: 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...

  7. 支付宝移动端 Hybrid 解决方案探索与实践

    支付宝 Hybrid 方案建设与演进 目前支付宝有 2 套 Hybrid 方案: HTML5 容器与小程序.小程序是最近几年才出来,H5 容器已经有了很长时间的历史,所以我们就先从 H5 容器说起. ...

  8. 基于MaxCompute的大数据安全方案

    ​简介:随着法律的完善,数据安全,信息安全,网络安全,升级成国家安全,所以数据安全不管对用户,还是对公司也都会变的越来越重要.做为大数据云数仓解决方案的领导者,阿里云MaxCompute在安全体系上也 ...

  9. 收藏!这些IDE使用技巧,你都知道吗

    简介: 欲善其事,先利其器.对于研发同学,在日常的开发工作中,我们与之打交道最多的便是编程的IDE.能否高效和灵活的使用IDE,将对我们的工作效率起着举足轻重的作用. 一 .背景 1 .目的 欲善其事 ...

  10. KubeDL 加入 CNCF Sandbox,加速 AI 产业云原生化

    ​简介: 2021 年 6 月 23 日,云原生计算基金会(CNCF)宣布通过全球 TOC 投票接纳 KubeDL 成为 CNCF Sandbox 项目.KubeDL 是阿里开源的基于 Kuberne ...