LeetCode 208 实现 Trie (前缀树)_208. 实现 trie (前缀树)_zhaohoutao的博客-程序员秘密

技术标签: Trie树  LeetCode  

实现 Trie (前缀树)

在这里插入图片描述

struct Node {
	bool isWord = false;
	Node* child[26] = {};
};
class Trie {
	
public:
    Node* root;
	/** Initialize your data structure here. */
	Trie() {
		root = new Node();
	}

	/** Inserts a word into the trie. */
	void insert(string word)
	{
		//使用字典树
		int len = word.size();
		Node* node = root;
		for (int i = 0; i < len; ++i)
		{
			char c = word[i];
			if (node->child[c - 'a'] == NULL)
			{
				node->child[c - 'a'] = new Node();
			}
			node = node->child[c - 'a'];
		}
		node->isWord = true;
	}

	/** Returns if the word is in the trie. */
	bool search(string word)
	{
		int len = word.size();
		Node* t = root;
		for (int i = 0; i < len&&t; ++i)
		{
			char c = word[i];		
			t = t->child[c - 'a'];
		}
		return t != NULL&&t->isWord;
	}

	/** Returns if there is any word in the trie that starts with the given prefix. */
	bool startsWith(string prefix)
	{
		int len = prefix.size();
		Node* t = root;
		for (int i = 0; i < len&&t; ++i)
		{
			char c = prefix[i];
			
			t = t->child[c - 'a'];
		}
		return t != NULL;
	}

};
/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhaohoutao/article/details/99871246

智能推荐

罗素“杀死了”康托尔_东方鹗的博客-程序员秘密

英国数学家罗素提出的著名的“罗素悖论”,直接证明了作为数学大厦基础的“集合论”是有问题的,这也导致了“集合论”的发现者康托尔一次又一次的经历着罗素的劫难却也解决不了这个问题,最终死在了自己工作的哈佛大学精神病院里面。更为严重的是,这引起了对数学的整个基础结构的有效性的置疑,也就是数学史上的第三次危机。集合理论集合是什么呢?用康托尔的话说,集合就是把具体的或思想上的一些确定的、彼此不同的对象...

使用python来实现零售行业的数据分析 : EDA+TF-IDF+t-SNE+K-Means+LDA(干货)_-派神-的博客-程序员秘密

当今电子商务已经非常普及,网上购物已经成为人们生活的一部分,电商网站上的商品数量已经呈现几何级的增长.伴随着在线的商品数量的增长,商品的定价越来越成为一个问题。比如服装的价格会呈现出季节性的变化趋势,而且受品牌的影响很大,而电子产品的价格则根据产品规格而波动。Mercari是一个日本C2C二手交易平台。他们们深深地了解零售商品定价这个问题。他们想向卖家提供定价建议,但这很难,因为他们的卖家可以...

SpringSecurity+JWT项目实战之Java权限管理实战(一)_daimeijin的博客-程序员秘密

前言本文是参考尚学堂SpringSecurity精讲,仅作为学习记录使用。这个系列设计到的技术点如下:SpringSecurityOauth2SpringSecurity + Oauth2SpringSecurity +JWTSpringSecurity + Oauth2SpringSecurity + Oauth2 + JWTSpringSecuritySpringSecurity简介Spring Security是一个高度自定义的安全框架。利用 Spring IoC/DI和AO

(转)STM32 FOC BLDC与PMSM的区别(https://blog.csdn.net/u010615629/article/details/50469721)_bldc foc 光盘 资料_dz093的博客-程序员秘密

BLDC:即无刷直流电机(Brushless Direct Current)PMSM:永磁同步电动机(Permanent-Magnet Synchronous Motor)二者结构上直接观察无明显区别,想要区分,看感应电动势从控制上由明显区别,PMSM感应电动势波形为正弦波,BLDC...

PTA 1014 福尔摩斯的约会 (c语言)_给个选择的博客-程序员秘密

1014 福尔摩斯的约会 (20 分)#include&lt;stdio.h&gt;#include&lt;string.h&gt;int main(){ int i,flag=0; char a[4][61],b[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"}; memset(a,'\0',sizeof(a)); for(i=...

c++中strstr函数的几种实现方法_c++abcdefg求cde_默伊清风的博客-程序员秘密

函数说明:包含文件:string.h函数名: strstr函数原型:extern char *strstr(char *str1, char *str2);功能:从字符串str1中查找是否有字符串str2, 如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。返回值:返回该位置的指针,如找不到,返回空指针。

随便推点

php 遍历文件夹并压成zip_PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载..._水稻偏爱者的博客-程序员秘密

PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流。这里整理一下常用的示例供参考。一、解压缩zip文件$zip = new ZipArchive;//新建一个ZipArchive的对象/*通过ZipArchive的对象处理zip文件$zi...

远程连接SQL Server 2008,服务器端和客户端配置_键盘tops舞者的博客-程序员秘密

远程连接SQL Server 2008,服务器端和客户端配置关键设置:第一步(SQL2005、SQL2008):开始-->程序-->Microsoft SQL Server 2008(或2005)-->配置工具-->SQL Server 配置管理器-->SQL Server网络配置-->MSSQLSERVER(这个名称以具体实例名为准) 的协议-->TCP/IP-->右键-

uni-app 组件使用 底部弹框_uni-app margin-bottom_Thanks-的博客-程序员秘密

&lt;template&gt; &lt;view&gt; &lt;view class="cover_screen" @click="hideBuyModal" v-if="showModalStatus"&gt;&lt;/view&gt; &lt;view class="buy_box" v-if="showModalStatus"&gt; &lt;text class="title"&gt;{{title}}&lt;/text&gt; &lt;text class="newde.

python中无法安装xpath库,Python爬虫 | xpath的安装_我的id是行的博客-程序员秘密

错误信息:程序包无效。详细信息:“Cannot load extension with file or directory name. Filenames starting with "" are reserved for use by the system.”。1、找到Chrome安装程序路径,找到对应的插件image.png2、把crx后缀名改为rar,解压缩得到文件夹(有错误提示不用理会)...

多维数据表达式MDX笔记_weixin_34357962的博客-程序员秘密

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

I/O端口和IO内存_fridayLL的博客-程序员秘密

转自 http://blog.csdn.net/bugouyonggan/article/details/8282981linux中的 IO端口映射和IO内存映射(一)地址的概念1)物理地址:CPU地址总线传来的地址,由硬件电路控制其具体含义。物理地址中很大一部分是留给内存条中的内存的,但也常被映射到其他存储器上 (如显存、BIOS等)。在程序指令中的虚拟地址经过段映射和页

推荐文章

热门文章

相关标签