LeetCode Word Search II

【LeetCode】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 阅读详情

LeetCode 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"].

题目的意思是给定二维字符表和一些单词,然后返回能在字符表中找到的单词的集合。

解法:由于可能有很多的单词需要查找,单词一多,就有很多共有的前缀(如“abcd”和“abcf”的前缀是“abc”),这样一来就没有必要每个单词从头找起,于是想到了前缀树的解决方法:即把单词构建成前缀树,然后在字符表搜索,遇到满足题意的就加入结果集。

class Trie {
public:
	Trie* branch[26];
	bool isWord;//从根节点到当前节点组成的字符串是否是列表中的一个单词
	int index;//如果是列表中的一个单词,记录该单词在列表中的下标
public:
	Trie()
	{
		memset(branch, 0, sizeof(branch));
		isWord = false;
		index = 0;
	}
};

Trie* buildTrie(vector<string>& words)
{
	Trie* root = new Trie();
	for (int i = 0; i < words.size(); i++)
	{	
		Trie* p = root;
		for (int j = 0; j < words[i].size(); j++)
		{
			if (p->branch[words[i][j] - 'a'] == NULL)
			{
				p->branch[words[i][j] - 'a'] = new Trie();
			}
			p = p->branch[words[i][j] - 'a'];
		}

		p->isWord = true;
		p->index = i;//记录在words中的下标
	}

	return root;
}

void dfs(vector<vector<char> >& board, int i, int j, int row, int col, Trie* root, vector<string>& res, vector<string>& words)
{	
	if (board[i][j] == 'X')
		return;
	if (root->branch[board[i][j] - 'a'] == NULL)
		return;
	else
	{
		char t = board[i][j];
		if (root->branch[t - 'a']->isWord == true)
		{
			res.push_back(words[root->branch[t - 'a']->index]);//把words中位于index的单词加入结果集
			root->branch[t - 'a']->isWord = false;//标记次单词已访问过
		}

		board[i][j] = 'X';//标记已经搜索过
		//上下左右进行递归搜索
		if (i > 0)			dfs(board, i - 1, j, row, col, root->branch[t - 'a'], res, words);		
		if ((i + 1)<row)	dfs(board, i + 1, j, row, col, root->branch[t - 'a'], res, words);
		if (j > 0)			dfs(board, i, j - 1, row, col, root->branch[t - 'a'], res, words);
		if ((j + 1)<col)	dfs(board, i, j + 1, row, col, root->branch[t - 'a'], res, words);
		
		board[i][j] = t; //回溯
	}
}

vector<string> findWords(vector<vector<char> >& board, vector<string>& words) 
{
	vector<string> res;
	int row = board.size();
	if (row == 0)	return res;
	int col = board[0].size();
	if (col == 0)	return res;

	int wordLen = words.size(); 
	if (wordLen == 0)	return res;

	Trie *root = buildTrie(words);//把单词列表构建成前缀树

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			//以每个字符作为起始点开始搜
			dfs(board, i, j, row, col, root, res, words);
		}
	}

	return res;
}


电源完整性仿真流程梳理 可根据仿真流程导图,一步一步的去设置完成仿真。仿真的关键,在于对电路回路的理解以及所放置的电压源与电流的合理性,对于去耦电容的选择,则需要根据谐振频率计算,从而选取有效滤波的容值。 阅读详情

相关推荐

拆开华为BBU5900,看看5G基站的大脑里到底装了啥?

本文深度拆解华为BBU5900,揭示5G基站核心处理单元的硬件架构与设计哲学。通过详细解析模块化设计、智能电源系统和动态散热方案,展现其支撑千兆速率、毫秒级时延的技术奥秘,为5G网络部署提供实用参考。

weixin_31456563的博客 559

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 hor

竹馨shine的专栏 423

虚拟化Esxi 和 华为交换机--配置链路捆绑

在这种模式下,Eth-Trunk的建立和成员接口的加入都需要手工配置,没有LACP协议的参与。● 在没有配置802.3ad聚合时,启用基于IP哈希的路由将中断网络,因此必须先对虚拟交换机进行更改。再去修改交换机的链路聚合模式(交换机配置为手动模式的链路聚合)当服务器需要通过交换机从远端服务器获取配置文件后才可以启动链路聚合时,交换机的链路聚合口需要配置lacp force-forward。● 交换机必须设置手动模式的链路聚合,虚拟交换机的负载平衡方法必须设置为基于IP哈希的路由。

ling 2113

[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 must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those hor

TstsUgeg的博客 1579

STM32多模式智能小车状态机设计与中断优先级实践

在嵌入式实时系统中,状态机是实现多任务协同与功能切换的核心范式。其本质是通过有限状态集合、明确转移条件和确定性动作,构建可预测、易维护的控制逻辑。基于STM32等资源受限MCU时,需兼顾状态调度效率、中断并发安全与硬件资源约束。关键技术价值体现在:提升系统模块解耦度、支持物理按键与蓝牙双源触发、保障实时响应不丢帧。典型应用场景包括智能小车多模运行(循迹、避障、遥控、定距跟随等)、工业设备工作模式切换及IoT终端低功耗状态管理。本文以STM32F103C8T6平台为例,深入解析全局模式变量设计、双通道状态切换

weixin_42509888的博客 93

微信小程序 - 最新获取用户昵称 / 头像(wx.getUserProfile 接口被废弃后的代替方案)详细教程,2022 年之后的所有微信小程序,获取用户信息最新详细教程,附带示例源代码

微信小程序wx.getUserProfile接口废了用什么代替,微信小程序wx.getUserProfile不能用了怎么办,wx.getUserProfile失效有什么替代方案吗,微信新规定头像昵称填写最新教程,目前微信小程序最新获取用户头像昵称教程,微信小程序用户头像昵称获取规则调整公告,2022改版后的微信小程序如何获取用户头像和昵称信息,微信小程序官方废弃getUserProfile后如何实现获取用户信息,微信小程序wx.getUserProfile接口返回了默认头像昵称,微信小程序获取用户信息不

🏆 前端领域优质创作者 2万+

212. Word Search II

212. Word Search II方法1: trie + backtracking 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 c...

wilzxu的博客 150

【YOLOv12改进trick】高效多尺度卷积注意力EMCAD_Block模块引入YOLOv12,实现涨点和效率提升,含创新点Python代码

EMCAD引入YOLOv12模块,实现高效多尺度注意力模块引入到YOLOv12中的快速涨点及效率提升

向上生长的程序媛的博客 851

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

yanqinghe123的专栏 527

海格通信嵌入式面试题及参考答案

计算电路的最高工作频率如何计算? 计算电路的最高工作频率主要考虑电路中的关键路径延迟。关键路径是指在整个电路中,信号传播延迟最长的路径。电路的最高工作频率的倒数就是时钟周期,而时钟周期必须大于关键路径的延迟时间。 首先要确定电路中各个模块的延迟。比如对于组合逻辑电路,可以通过查看芯片手册获取门电路的延迟参数。以一个简单的与门、或门、非门组成的组合逻辑为例,每个门都有自己的传输延迟,把信号从输入到输出经过的所有门的延迟相加,就得到这部分组合逻辑的延迟。 对于包含寄存器的电路,还需要考虑寄

大模型大数据攻城狮的专栏 1271

**Coffin-Manson 模型**在半导体功率循环测试中的理论背景、数学形式、应用场景,以及如何使用 **C#** 编程语言进行具体实现

Coffin-Manson 模型最初用于描述金属材料在低周疲劳(Low-Cycle Fatigue, LCF)下的寿命,后来被广泛应用于半导体功率器件(如 IGBT、MOSFET)的可靠性分析,特别是在功率循环测试中。如果你有具体数据(如 CSV 文件)、更复杂的模型要求(如 Bayerer 模型)或特定可视化需求,请提供,我可以进一步优化代码或添加功能!以下代码实现基本模型 ( N_f = A \cdot (\Delta T_j)^{-n} ) 的拟合,生成拟合曲线并预测寿命。我会结合你之前提到的。

专注于前沿技术的探索与实践,热衷于从代码逻辑、架构设计到行业应用等多维度深挖技术本质。期待在技术之路上与你共同成长~ 480

[leetcode] 212. Word Search II 解题报告

题目链接: https://leetcode.com/problems/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 sequent

小榕流光的专栏 3716

YOLOv8训练流程-原理解析[目标检测理论篇]

本篇文章主要介绍了YOLOv8训练的流程,主要介绍了Task Aligned Assigner正负样本匹配策略以及YOLOv8的损失计算。

ice 4516

leetcode -- Word Search II -- 难理解,再看看

https://leetcode.com/problems/word-search-ii/最直观的思路就是对于每个word,都对board进行一次dfs搜索。这样在words太多的情况下会TLE. 这里考虑到board上有些point,对于任意word或者任意word的后缀都没有再继续搜索的必要,所以这里采用trie字典树存储所有带搜索的word。这样就只用dfs board一遍,就可以把所有的在b

xyqzki的专栏 901

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【Leetcode】212. Word Search II

思路是DFS + Trie。想到DFS是自然的,由于要搜索多个字符串,为了避免多次重复搜索,我们可以在DFS的时候带着Trie节点一起搜,相当于同时在矩阵和Trie里搜索,当同时搜到某个单词的时候,就可以加入结果了。给定一个二维字符矩阵,再给定若干单词,问是否存在矩阵中的某条路径使得这个路径上字符组成的单词恰好就是那几个单词之一,要求返回所有满足条件的单词。路径不允许走到相同格子上。时间复杂度与具体情况有关,比较难分析。为words里单词总长度)。

数学、算法爱好者的博客 503

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 212. Word Search II(dfs + 字典树)

题目来源:https://leetcode.com/problems/word-search-ii/ 问题描述 212.Word Search II Hard Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be construct...

da_kao_la的博客 197

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Leetcode - Word Search I,II

Leetcode - 079. Word Search 我的思路:1.首先得在board中寻找首元素可能出现的位置,对每个合法的开始位置进行dfs2.每次dfs的过程中,结合backtracing避免回退 class Solution { public: void dfs(vector<vector<char>...

weixin_34248023的博客 150

LeetCode Top Interview Questions 212. Word Search II (Java版; Hard)

welcome to my blog LeetCode Top Interview Questions 212. Word Search II (Java版; Hard) 题目描述 Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be con...

littlehaes的博客 180

Leetcode 212. Word Search II

【代码】Leetcode 212. Word Search II。

小白菜又菜 532

[一起来刷leetcode吧][3]--No.212 Word Search II.md

这篇文章是程序自动发表的,详情可以见这里— categories: leetcode tags: [python,leetcode,os] channel:16 这篇文章是程序自动生成并发表的,详情见这里这是leetcode的第212题–Word Search II   题目 Given a 2D board and a list of words from the diction...

mbinary 166

Leetcode212. Word Search II-前缀树Trie

Leetcode212. Word Search IILeetcode212. Word Search II题目思路复杂度代码 Leetcode212. Word Search II 题目 题目链接 思路 将词典构建为Trie, 对board中的每个字母,进行dfs遍历,检查是否在Trie中 复杂度 设词典最长词的长度为L,board有M行N列 时间复杂度O(n)\mathcal{O}(n)O(n) 最坏情况,board中的每个字母,向四个方向都能不停的匹配下去,直到达到字典中的单词长度,如字典为aaa

qq_17065591的博客 345

leetcode 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 horizo

不积跬步,无以至千里 1802

LeetCode OJ 之 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

灯火阑珊处 1878

[LeetCode] 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 t...

weixin_30767921的博客 81

[Lintcode]132. Word Search II /[Leetcode]212. Word Search II

132. Word Search II / 212. Word Search II 本题难度: Hard Topic: Data Structure - Tire/DFS Description Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus...

siriusli1981的博客 229
上一篇: LeetCode Add and Search Word - Data structure design
下一篇: LeetCode Course Schedule
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值