2021-09-05:单词搜索 II。给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。力扣212。

福大大 答案2021-09-05:

前缀树。

代码用golang编写。代码如下:

package main

import "fmt"

func main() {
board := [][]byte{{'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}}
words := []string{"oath", "pea", "eat", "rain"}
ret := findWords(board, words)
fmt.Println(ret)
} type TrieNode struct {
nexts []*TrieNode
pass int
end bool
} func NewTrieNode() *TrieNode {
ans := &TrieNode{}
ans.nexts = make([]*TrieNode, 26)
ans.pass = 0
ans.end = false
return ans
} func fillWord(head *TrieNode, word string) {
head.pass++
chs := []byte(word)
index := 0
node := head
for i := 0; i < len(chs); i++ {
index = int(chs[i] - 'a')
if node.nexts[index] == nil {
node.nexts[index] = NewTrieNode()
}
node = node.nexts[index]
node.pass++
}
node.end = true
} func generatePath(path []byte) string { //LinkedList<Character>
str := make([]byte, len(path))
index := 0
for _, cha := range path {
str[index] = cha
index++
}
return string(str)
} func findWords(board [][]byte, words []string) []string {
head := NewTrieNode() // 前缀树最顶端的头
set := make(map[string]struct{})
for _, word := range words {
if _, ok := set[word]; !ok {
fillWord(head, word)
set[word] = struct{}{}
}
}
// 答案
ans := make([]string, 0)
// 沿途走过的字符,收集起来,存在path里
path := make([]byte, 0)
for row := 0; row < len(board); row++ {
for col := 0; col < len(board[0]); col++ {
// 枚举在board中的所有位置
// 每一个位置出发的情况下,答案都收集
process(board, row, col, &path, head, &ans)
}
}
return ans
} // 从board[row][col]位置的字符出发,
// 之前的路径上,走过的字符,记录在path里
// cur还没有登上,有待检查能不能登上去的前缀树的节点
// 如果找到words中的某个str,就记录在 res里
// 返回值,从row,col 出发,一共找到了多少个str
func process(board [][]byte, row int, col int, path *[]byte, cur *TrieNode, res *[]string) int {
cha := board[row][col]
if cha == 0 { // 这个row col位置是之前走过的位置
return 0
}
// (row,col) 不是回头路 cha 有效 index := cha - 'a'
// 如果没路,或者这条路上最终的字符串之前加入过结果里
if cur.nexts[index] == nil || cur.nexts[index].pass == 0 {
return 0
}
// 没有走回头路且能登上去
cur = cur.nexts[index]
*path = append(*path, cha) // 当前位置的字符加到路径里去
fix := 0 // 从row和col位置出发,后续一共搞定了多少答案
// 当我来到row col位置,如果决定不往后走了。是不是已经搞定了某个字符串了
if cur.end {
*res = append(*res, generatePath(*path))
cur.end = false
fix++
}
// 往上、下、左、右,四个方向尝试
board[row][col] = 0
if row > 0 {
fix += process(board, row-1, col, path, cur, res)
}
if row < len(board)-1 {
fix += process(board, row+1, col, path, cur, res)
}
if col > 0 {
fix += process(board, row, col-1, path, cur, res)
}
if col < len(board[0])-1 {
fix += process(board, row, col+1, path, cur, res)
}
board[row][col] = cha
//path.pollLast()
*path = (*path)[0 : len(*path)-1]
cur.pass -= fix
return fix
}

执行结果如下:


左神java代码

2021-09-05:单词搜索 II。给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。单词必须按照字母顺序,通过 相邻的的更多相关文章

  1. Leetcode 212.单词搜索II

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

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

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

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

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

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

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

  5. 212. 单词搜索 II

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

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

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

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

  8. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  9. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  10. 单词搜索 II · Word Search II

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

随机推荐

  1. 看图就会-网络攻击 (xss和csrf)

    最近发现好多东西整理过后就没啥印象,但是思维导图很好用,能取其精华去其糟粕

  2. redis的数据操作和python操作redis+关系非关系数据库差异

    关系型数据库(RMDBS) 数据库中表与表的数据之间存在某种关联的内在关系,因为这种关系,所以我们称这种数据库为关系型数据库. 典型:Mysql/MariaDB.postgreSQL.Oracle.S ...

  3. 【转载】Python:logging详细版

    转载自:https://www.cnblogs.com/Nicholas0707/p/9021672.html 一.logging模块 (一).日志相关概念 日志是一种可以追踪某些软件运行时所发生事件 ...

  4. linux网络编程中的errno处理

    在Linux网络编程中,errno是一个非常重要的变量.它记录了最近发生的系统调用错误代码.在编写网络应用程序时,合理处理errno可以帮助我们更好地了解程序出现的问题并进行调试. 通常,在Linux ...

  5. T-Dubbo,最好的RPC接口测试工具,支持nacos、zookeeper两大主流注册中心,真香!

    这可能是有史以来最好用的RPC接口测试工具 文末有视频简介 获取方式 一只小Coder 简介 T-Dubbo,是一个基于Dubbo的全自动RPC接口测试平台为当下最流行的微服务架构中的RPC接口提供了 ...

  6. python入门教程之二十邮件操作

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...

  7. pysimplegui之使用多线程,避免程序卡死

    这个问题我也遇到过,就是还需要一个while循环的时候,放到gui本身循环会卡死,这时候就需要启动多线程 需要"长时间"的操作 如果您是 Windows 用户,您会在其标题栏中看到 ...

  8. python中文文档

    这是在线中文文档 https://docs.python.org/zh-cn/3.7/library/winreg.html

  9. [Nginx/Linux]Nginx从1.15.12平滑升级到1.17.5

    1 问题背景 nginx 安全漏洞(CVE-2019-9511) nginx 安全漏洞(CVE-2019-9513) nginx 安全漏洞(CVE-2019-9516) http://www.cnnv ...

  10. [Nginx]安装第三方调试模块——echo | #解决异常#unknown directive “echo”

    前言 echo 模块/指令: 在Nginx中是一个第三方开发者----agentzh(章亦春)开发的.功能强大的调试工具. location = /helloworld/ { default_type ...