Q:

给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

示例:

输入:

words = [“oath”,“pea”,“eat”,“rain”] and board =

[

[‘o’,‘a’,‘a’,‘n’],

[‘e’,‘t’,‘a’,‘e’],

[‘i’,‘h’,‘k’,‘r’],

[‘i’,‘f’,‘l’,‘v’]

]

输出: [“eat”,“oath”]

说明:

你可以假设所有输入都由小写字母 a-z 组成。

提示:

你需要优化回溯算法以通过更大数据量的测试。你能否早点停止回溯?

如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。什么样的数据结构可以有效地执行这样的操作?散列表是否可行?为什么? 前缀树如何?如果你想学习如何实现一个基本的前缀树,请先查看这个问题: 实现Trie(前缀树)。

A:

对给定单词表建立前缀树,从矩阵每个位置开始向上下左右dfs寻找,当前遍历过的位置置为’$’,成功查找到的单词在前缀树中做相应记录,防止之后的dfs再次查找该单词。

class Solution:
def findWords(self, board, words):
rows=len(board)
if not rows:
return []
cols=len(board[0])
trie={}
res=[]
for word in words:
t=trie
for x in word:
if x not in t:
t.setdefault(x,{})
t=t[x]
t['end']=1
def dfs(i,j,cur_word,node):
nonlocal rows,cols,res
c=board[i][j]
if c not in node:
return
node=node[c]
if 'end' in node:
if node['end']==1:
res.append(cur_word+c)
node['end']=0
board[i][j]='$'
for ii,jj in [(0,1),(0,-1),(1,0),(-1,0)]:
xx,yy=i+ii,j+jj
if 0<=xx<rows and 0<=yy<cols and board[xx][yy]!='$':
dfs(xx,yy,cur_word+c,node)
board[i][j]=c
for i in range(rows):
for j in range(cols):
dfs(i,j,'',trie)
return res

212. 单词搜索 II的更多相关文章

  1. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  2. [LeetCode] 212. 单词搜索 II

    题目链接:https://leetcode-cn.com/problems/word-search-ii/ 题目描述: 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在 ...

  3. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  4. [leetcode] 212. 单词搜索 II(Java)

    212. 单词搜索 II 这leetcode的评判机绝对有问题!!同样的代码提交,有时却超时!害得我至少浪费两个小时来寻找更优的答案= =,其实第一次写完的代码就可以过了,靠!!!第207位做出来的 ...

  5. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...

  6. Java实现 LeetCode 212 单词搜索 II

    public class Find2 { public int[] dx={1,-1,0,0}; public int[] dy={0,0,1,-1}; class Trie{ Trie[] trie ...

  7. [Swift]LeetCode212. 单词搜索 II | 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. 单词搜索 II · Word Search II

    [抄题]: 给出一个由小写字母组成的矩阵和一个字典.找出所有同时在字典和矩阵中出现的单词.一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动. 给出矩阵: doafagaidca ...

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

随机推荐

  1. RHEL 8 安装 Oracle 19c 注意问题

    RedHat Enterprise Linux 8 版本静默安装 Oracle 数据库软件时,需注意的问题 来自博客园AskScuti 1. 提示缺少库文件 libnsl.so.1 2. 因着OS版本 ...

  2. Django生成脚本迁移文件时,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    一.本人环境:django:3.0.2, python:3.8.1,  pymysql:0.9.3 二.解决步骤: 1.django目录下找到 base.py文件: 2.在base.py文件中注释以下 ...

  3. 大话STM32F103系统架构

    前言 许多像我一样的STM32初学者,都往往忽视了STM32系统架构的学习.这对于实际应用并没有啥大的影响,但是总感觉怎么学也无法看清STM32的全貌,所以本文我将带领大家一起厘清STM32F103的 ...

  4. LaTeX技巧003:实现一个章标题

    代码如下: \documentclass[11pt]{book} \usepackage{graphicx} \usepackage{CJKfntef} \usepackage{color} \mak ...

  5. 记录 Docker 的学习过程 (网络篇之跨主机互通)

    下面从node3上操作node3# docker run -d -p 8500:8500 --name consul progrium/consul -server -bootstrap node3# ...

  6. 使用Echarts实现折线图的一点总结

    使用Echarts的一点总结 1.安装,我使用得vue cnpm install echarts --save 2.在入口文件main.js中注册,并使用 // 引入折线图 echarts impor ...

  7. AcWing 272. 最长公共上升子序列

    #include<iostream> using namespace std ; ; int n; int a[N]; int b[N]; int f[N][N]; //f[i][j] / ...

  8. 函数的应用_python

    一.各类函数应用 1.高阶函数:函数里面调用函数 例子: def add(a): return (a*a) def print_info(fun,c,d): #传入函数与参数 return fun(c ...

  9. lamda表达式的两种写法

    public class Test{ public synchronized void test1(){ System.out.println("test1 start........... ...

  10. java内部类概念

    一.成员内部类作为外部类的成员存在的类,则称之为成员内部类 public class OuterClass{ public class InnerClass{ } } 成员内部类样例 成员内部类具有如 ...