LeetCode Add and Search Word - Data structure design

11. 使用 HTTP PUT, PATCH 以及 MERGE 请求消费 SAP ABAP OData 服务修改操作的实现及其区别 使用 SAP ABAP SEGW 事物码实现 OData 服务的修改操作时,对于消费端通过 PATCH/MERGE 和 PUT 这三种不同类型的 HTTP 请求触发的场景,我们应该在 UPDATE_ENTITY 方法里用 IF 分支分别进行处理,原因就在于 GET_ENTITY 方法是否在修改处理逻辑的过程中被触发。...... 阅读详情

Add and Search Word - Data structure design 

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.


这个题目是字典树的扩展,之前有个 构建字典树的题目,此题知识按照需求增加一次搜索。


class TrieNode {
public:
	// Initialize your data structure here.
	TrieNode() 
	{
		memset(branch, 0, sizeof(branch));
		isWord = false;
	}

	TrieNode* branch[26];//记录26个小写字母的分支
	bool isWord;//判断某个分支节点是不是单词(比如插入abcd,只有abcd是单词,而abc只是前缀,不是单词;用于辅助search方法)
};

class WordDictionary {
public:
	//构造函数
	WordDictionary()
	{
		root = new TrieNode();
	}

	// Adds a word into the data structure.
	void addWord(string word)
	{
		TrieNode* p = root;
		for (int i = 0; i < word.length(); i++)
		{
			if (p->branch[word[i] - 'a'] == NULL)
			{
				p->branch[word[i] - 'a'] = new TrieNode();
			}

			p = p->branch[word[i] - 'a'];
		}
		//插入“是单词”的标记
		p->isWord = true;		

	}

	// Returns if the word is in the data structure. A word could
	// contain the dot character '.' to represent any one letter.
	bool search(string word)
	{
		
		return MySearch(word, root);
	}

	bool MySearch(string word, TrieNode* p)
	{
		if (word.empty())
			return p->isWord;

		int index = word[0] - 'a';

		if (word[0] == '.')
		{
			for (int i = 0; i < 26; i++)
			{
				if (p->branch[i] == NULL)
					continue;
				//递归,搜索子串,分支
				if (MySearch(word.substr(1, word.size() - 1), p->branch[i]))
					return true;
			}
			return false;
		}
		else if (p->branch[index] != NULL)
		{
			if (word.size() > 1)
				return MySearch(word.substr(1, word.size() - 1), p->branch[index]);
			else
				return p->branch[index]->isWord;
		}

		return false;
	}

private:
	TrieNode* root;
};





[PCIe5.0] 5.3.1.4.2 D3Cold State 主电源(Vcc)完全断开,仅保留极少量辅助电源(Vaux)用于维持基本唤醒功能设备功能完全关闭,所有寄存器状态丢失(除非有特殊存储)需要完全重新初始化才能恢复工作比D3Hot状态功耗更低,但唤醒延迟更长。 阅读详情

相关推荐

Anomalib实战:从零开始构建自定义数据集的异常检测模型

本文详细介绍了如何使用Anomalib 2.1.0从零开始构建自定义数据集的异常检测模型。通过实战演示,指导读者准备符合规范的数据集结构,快速上手训练Patchcore等先进算法,并完成模型推理与调优,为工业质检、医疗影像等场景提供高效的视觉异常检测解决方案。

weixin_29165465的博客 109

【Leetcode】Add and Search Word - Data structure design

题目链接:https://leetcode.com/problems/add-and-search-word-data-structure-design/ 题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) se

业精于勤,荒于嬉 1330

华为数通---配置风暴控制案例

风暴控制可以通过阻塞报文或关闭端口来阻断广播、未知组播和未知单播报文的流量。

2301_77081516的博客 1624

leetcode 211: Add and Search Word - Data structure design

Add and Search Word - Data structure design leetcode 211

西施豆腐渣 7972

从零玩转GD32单片机CAN总线:手把手实战配置与源码解析

本文提供了一份基于GD32单片机的CAN总线实战入门指南。从硬件连接(GD32F103C8T6与TJA1050收发器)到软件配置,详细解析了CAN初始化、波特率计算、过滤器设置及中断收发等核心源码。通过手把手的回环测试示例与常见问题排查,帮助开发者快速掌握CAN总线通信的关键配置与调试技巧。

g5h6i7j的博客 481

【校园网代理连接问题】已解决,“该设备或资源(Web 代理)未设置为接受端口“7890”上的连接“

我遇到这个问题很多次。每次都是vpn的问题,不止通俗意义上的梯子(clash之类的),还包括校园网vpn。点击“ 拨号和虚拟专用网络设置”中的“设置”, 关闭代理服务器。在打开vpn后或者关闭vpn后无法浏览网页,网页诊断显示“关闭“手动设置代理”中的“使用代理服务器”,关闭对应的本地代理即可。

聆雨。 1万+

leetcode——Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word

stillingpb的博客 480

小白跑WRF第三天,安装三方依赖库

小白跑WRF第三天,安装三方依赖库各类安装包的准备 各类安装包的准备 官网教程:https://www2.mmm.ucar.edu/wrf/OnLineTutorial/compilation_tutorial.php 根据官网教程需要的安装包有以下几个 系统环境 用途 zlib-1.2.7 zlib是提供数据压缩用的函式库。 netcdf-4.1.3 读nc文件的。 mpich-3.0.4 用于并行运算的工具。 libpng-1.2.50 libpng是一款C语言编写的比较底层

weixin_42372313的博客 1万+

[leetcode] 211. Add and Search Word - Data structure design

Description Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing o...

Learning from the mistakes 326

Windows安装WSL子系统及docker,以及WSL和docker配置、使用及问题解决

在Windows操作系统中,Ubuntu子系统(也称为Windows Subsystem for Linux, WSL)为开发者提供了一个在Windows环境下运行Linux环境的平台。然而,有时用户在按照Ubuntu子系统或者使用WSL时,可能会遇到各种问题,下面总结一下解决方式。想要在Windows上安装Docker(实际上是基于Hyper-V或者WSL2这两项虚拟化技术),需要开启虚拟化功能,所以不管是对于系统还是硬件都有一定的要求。

sym的博客 9780

【BUG已解决】RuntimeError: CUDA error: no kernel image is available for execution on the device 解决方案

方案适用场景推荐指数精确匹配官方版本绝大多数场景(大众消费卡+主流数据中心卡)⭐⭐⭐⭐⭐安装历史旧版本Pascal/Maxwell等老显卡⭐⭐⭐⭐安装Nightly版本刚发布的最新架构显卡⭐⭐⭐⭐源码编译官方各版本均不支持的特殊硬件⭐⭐Docker官方镜像追求省心、避免版本排查⭐⭐⭐⭐⭐的核心是PyTorch 预编译包不包含你显卡对应的计算能力架构先确认显卡真实计算能力),而不是盲目重装驱动对比 PyTorch 支持的架构列表),定位是否真的不匹配精确匹配官方版本。

博客 1万+

[Leetcode 211, Medium] Add and Search Word - Data structure design

[Leetcode 211, Medium] Add and Search Word - Data structure design

算法的实践 744

基于FPGA的相控阵雷达波束控制系统设计(2)

也就是在计算波控码时0,按2.8125递增,取遍-90

qq_43416206的博客 1810

[Leetcode] 211. Add and Search Word - Data structure design

Problems: https://leetcode.com/problems/add-and-search-word-data-structure-design/ Solution: 本题如果使用hashmap的话,在search时将会很难应付“…ab”的情况 class WordDictionary { class TrieNode { boolean isTrie;...

Renoler的博客 154

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

[leetcode] 211 Add and Search Word - Data structure design

因为给定了a-z这个范围,并且字符串的添加和查找符合Trie的常用方法,因此考虑使用Trie这种数据结构。 然后和普通的Trie不同的是,要匹配正则表达式中的“.”,也就是说在这一层是无法判断沿着拿个结点向下走的,所以要循环这一层的结点,只有这一层所有结果失败后才能返回false,剩下的递推。所以我们采取Trie+回溯法。 代码中的searchHelp函数是专门用于回溯的,要求会回溯的掌握比较

NK_test的博客 1882

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[leetcode]Add and Search Word - Data structure design

Add and Search Word - Data structure design   Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word

不积跬步无以至千里 565

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode之Add and Search Word - Data structure design

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only ...

qq_35455503的博客 171

Leetcode 211 Add and Search Word - Data structure design

设计一个数据结构来支持以下两个操作:search(word)既可以查找一个字面上的直接量,也可以查找一个包含a-z和'.'的正则表达式,'.'表示任意字符。

(ง •̀_•́)ง 804

leetcode Add and Search Word - Data structure design

题目Design a data structure that supports the following two operations:void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters

不积跬步,无以至千里 1721

[LeetCode] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only lett

小熊听 431

LeetCode211 Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only lette...

李歇特冯-兹拜因巴哈的博客 205

[Leetcode]Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letter

dyllanzhou的专栏 302

[LeetCode]Add and Search Word - Data structure design

字典树( Trie ) 推荐一个对字典树解释的很好的博客 Trie树:应用于统计和排序  解题思路: 阅读完题目,大家都知道是字典树。对于树的搜索算法有 深度优先搜索算法(dfs)和宽度优先搜索算法(bfs)。针对题目的描述,只要能找到一个匹配的,即可返回true。所以,采用dfs,深度挖掘有用信息,只要发现pattern匹配,就结束搜索。(bfs在这里会产生很多冗余的搜索,浪费

Leslie的专栏 533
上一篇: LeetCode Implement Trie (Prefix Tree)
下一篇: LeetCode Word Search II
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值