[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 ...
随机推荐
- serialize()传值缺失
思路:serialize()获取的是 " & " 拼接的字符串,无法传值,需要拆分后,拼接,生成新字符串,传过去. 例子: var v_idd = $("form ...
- InnoDB 引擎独立表空间
InnoDB 引擎独立表空间 使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到 ...
- 基于Django-Cookie的CBV和FBV的用户验证装饰器
FBV模式 def cookie(func): def deco(request,*args,**kwargs): u = request.get_signed_c ...
- 升级lamp中php5.6到php7.0过程
升级过程我就直接摘录博友,http://www.tangshuang.net/1765.html,几乎问题和解决办法都是参照他的,所以我也就不另外写了.谢谢!! 周末看了一下php7的一些情况,被其强 ...
- 10种CSS3实现的Loading效果
原文链接:http://www.cnblogs.com/jr1993/p/4622039.html 第一种效果: 代码如下: <div class="loading"> ...
- Python使用OpenCV实现简单的人脸检测
文章目录: OpenCV安装 安装numpy 安装opencv OpenCV使用 OpenCV测试 效果图: 注意: 图片人脸检测 程序要求: 技术实现思路 注意 本文使用的环境是:Windows+P ...
- raw数据类型
Oracle中用于保存位串的数据类型是RAW,LONG RAW(推荐使用BLOB). RAW,类似于CHAR,声明方式RAW(L),L为长度,以字节为单位,作为数据库列最大2000,作为变量最大327 ...
- Python 使用 Redis 操作
1.redis简介 redis是一款开源免费的高性能key-value数据库,redis特点: 支持更多的数据类型:字符串(String).列表(List).哈希(Map).数字(Int).集合(Se ...
- 磁盘性能分析之iotop
一.安装. yum install iotop [root@localhost tmp]# iotop -o iotop命令的键盘快捷键: 1.左右箭头改变排序方式,默认是按IO排序 2.r键是反向排 ...
- vue总结 06组件
组件基础 基本示例 这里有一个 Vue 组件的示例: // 定义一个名为 button-counter 的新组件Vue.component('button-counter', { data: func ...