[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 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的更多相关文章
- 【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 ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 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 ...
- 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 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 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#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 ...
随机推荐
- weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )
~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...
- console.dir() 与 console.dirxml() 的使用
在调试JavaScript程序时,有时需要dump某些对象的详细信息.通过手工编写JavaScript代码可以完成这一工作:针对对象的属性进行循环,将循环到的每一个属性值打印出来:可见,这一过程是比较 ...
- 20165320 预备作业2:技能学习心得与C语言学习
一.技能学习心得 1.你有什么技能比大多数人好? 我觉得我的篮球打得比一般的人好吧,但是也仅仅掌握了大部分基本的篮球技巧,算不上精通. 2.针对这个技能的获取你有什么成功的经验? 我觉得要打好篮球需要 ...
- 【算法学习】老算法,新姿势,STL——Heap
“堆”是一个大家很熟悉的数据结构,它可以在\(O(log\;n)\)的时间内维护集合的极值. 这都是老套路了,具体的内部实现我也就不谈了. 我一般来说,都是用queue库中的priority_queu ...
- Linux configure关于交叉编译的参数设置【转】
转自:http://blog.csdn.net/darennet/article/details/9003005 configure的参数众多,一般包括如下 --srcdir=DIR 这个选项对安装没 ...
- Linux的bg和fg命令 ---让程序在前台后台之间切换
Linux的bg和fg命令 我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命 ...
- 删除git库中untracked files(未监控)的文件
https://blog.csdn.net/ronnyjiang/article/details/53507306 在编译git库拉下来的代码时,往往会产生一些中间文件,这些文件我们根本不需要,尤其是 ...
- 洛谷P2458 保安站岗
传送门啦 分析: 树形dp刚刚入门,这是我做的第一个一个点同时受父亲节点和儿子节点控制的题目. 由于这个题中某一个点放不放保安与父亲和儿子都有关系(因为线段的两个端点嘛),所以我们做题时就要考虑全面. ...
- opencv(1)图像处理
2.图像操作 图片裁剪 裁剪是利用array自身的下标截取实现 HSV空间 除了区域,图像本身的属性操作也非常多,比如可以通过HSV空间对色调和明暗进行调节.HSV空间是由美国的图形学专家A. R. ...
- SQL SERVER 触发器介绍
什么是触发器 触发器对表进行插入.更新.删除的时候会自动执行的特殊存储过程.触发器一般用在check约束更加复杂的约束上面.触发器和普通的存储过程的区别是:触发器是当对某一个表进行操作.诸如:upda ...