Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must 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 in a word.

For example,
Given words = ["oath","pea","eat","rain"] and board =

[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]

Return ["eat","oath"].

题意:查找哪些单词在二维字母数组中出现

思路:1.用所有的单词建立字典树

   2.遍历二维数组中的所有位置,以这个位置开头向二维数组上下左右方向扩展的字符串是否在字典树中出现

  评论区还有一种用复数和字典树的解题方法,太帅了

 class Solution(object):
def findWords(self, board, words):
trie = {}
for w in words:
cur = trie
for c in w:
cur = cur.setdefault(c,{})
cur[None] = None
self.flag = [[False]*len(board[0]) for _ in range(len(board))]
self.res = set()
for i in range(len(board)):
for j in range(len(board[0])):
self.find(board,trie,i,j,'')
return list(self.res) def find(self,board,trie,i,j,str):
if None in trie:
self.res.add(str)
if i<0 or i>=len(board) or j<0 or j>=len(board[0]):
return
if not self.flag[i][j] and board[i][j] in trie:
self.flag[i][j] = True
self.find(board,trie[board[i][j]],i-1,j,str+board[i][j])
self.find(board,trie[board[i][j]],i+1,j,str+board[i][j])
self.find(board,trie[board[i][j]],i,j+1,str+board[i][j])
self.find(board,trie[board[i][j]],i,j-1,str+board[i][j])
self.flag[i][j] = False
return

[leetcode trie]212. Word Search II的更多相关文章

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

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

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

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

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

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

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

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

  7. 212. Word Search II

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

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

  9. 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. 【AtCoder Grand Contest 007E】Shik and Travel [Dfs][二分答案]

    Shik and Travel Time Limit: 50 Sec  Memory Limit: 512 MB Description 给定一棵n个点的树,保证一个点出度为2/0. 遍历一遍,要求每 ...

  2. C语言入门教程-(2)基本程序结构

    1.简单的C语言程序结构 要建造房屋,首先需要打地基.搬砖搭建框架(这大概就是为什么叫搬砖的原因).学习计算机语言的时候也一样,应该从基本的结构开始学起.下面,我们看一段简单的源代码,这段代码希望大家 ...

  3. Java内存模型简析

    1.多线程基础 线程通信,是指线程之间以何种机制来交换信息.其中通信的机制有两种:内存共享和消息传递.内存共享是指线程之间通过写-读内存中的公共状态隐式进行通讯(Java):消息传递在线程之间没有公共 ...

  4. 灰色预测 GM11模型

    灰色预测实现见:https://www.jianshu.com/p/a35ba96d852b from pandas import Series from pandas import DataFram ...

  5. 【Tomcat】Tomcat配置与优化(内存、并发、管理)【自己配置】

    一.JVM内存配置优化 主要通过以下的几个jvm参数来设置堆内存的: -Xmx512m 最大总堆内存,一般设置为物理内存的1/4 -Xms512m 初始总堆内存,一般将它设置的和最大堆内存一样大,这样 ...

  6. JUnit基本介绍

    一.什么是单元测试 单元测试(Unit  Testing)是指在计算机编程中,针对程序模块来进行正确性检验的测试工作.单元测试的特点如下: ※ 程序单元是应用最小的可测试部件,通常采用基于类或者类的方 ...

  7. python基础--subprocess模块

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  8. Nginx - Header详解

    1. 前言 通过 HttpHeadersModule 模块可以设置HTTP头,但是不能重写已经存在的头,比如可能相对server头进行重写,可以添加其他的头,例如:Cache-Control,设置生存 ...

  9. 学习shell脚本之前的基础知识

    日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...

  10. 记一次对 Laravel-permission 项目的性能优化

    我最近研究分析了在 SWIS上面创建的项目的性能.令人惊讶的是,最耗费性能的方法之一是优秀的  spatie/laravel-permission 包造成的. 经过查阅更多资料和研究,发现一个可能明显 ...