当前位置:网站首页>力扣208,实现Trie(前缀树)
力扣208,实现Trie(前缀树)
2022-08-09 07:02:00 【瀛台夜雪】
力扣208,实现Trie(前缀树)
题目描述
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
Trie() 初始化前缀树对象。
void insert(String word) 向前缀树中插入字符串 word 。
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
输入输出样例
输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]
解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 True
trie.search("app"); // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app"); // 返回 True
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-trie-prefix-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
tips
1 <= word.length, prefix.length <= 2000
word 和 prefix 仅由小写英文字母组成
insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
解法:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//Trie,又称为前缀树或字典树,是一颗有限数
//指向子节点的指针数组next.即小写英文字母的数量
//isend,表示该结点是否为字符串的结尾
class Trie{
private:
bool isEnd;
Trie *next[26];
public:
Trie()
{
isEnd=false;
//memset 将某一块内存中的全部设置为指定的值
memset(next,0,sizeof(next));
}
//首先从根节点的子节点开始和word第一个字符进行匹配,一直匹配到前缀链上没有对应的字符
//此时不断开辟新的结点,直到插入完word的最后一个字符,并将最后一个结点的isend=true, 表明是但此的末尾
void insert(string word)
{
Trie*node=this;
for(char c:word)
{
if(node->next[c-'a']==NULL)
{
node->next[c-'a']=new Trie();
}
node=node->next[c-'a'];
}
node->isEnd=true;
}
//从根结点的子节点开始,一直向下进行匹配,如果出现结点值为空就返回false,如果
//待搜索的结点已经到最后一个值,那么只需看最后一个结点的isEnd的状态即可
bool search(string word)
{
Trie*node=this;
for(char c:word)
{
node=node->next[c-'a'];
if(node==NULL)
{
return false;
}
}
return node->isEnd;
}
//和查找的函数类似,但是最后判断无需判断最后一个结点的isend,因为是前缀
bool startsWith(string prefix)
{
Trie *node=this;
for(char c:prefix)
{
node=node->next[c-'a'];
if(node==NULL)
{
return false;
}
}
return true;
}
};
int main()
{
Trie* trie=new Trie();
trie->insert("apple");
trie->search("apple"); // 返回 True
trie->search("app"); // 返回 False
trie->startsWith("app"); // 返回 True
trie->insert("app");
trie->search("app"); // 返回 True
}
边栏推荐
猜你喜欢

虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection

jmeter并发数量以及压力机的一些限制

灵活好用的sql monitoring 脚本 part7

细谈VR全景:数字营销时代的宠儿

leetcode 之盛水问题

C语言的内置宏(定义日志宏)

差分约束-图论

Quectel EC20 4G module dial related

Import the pycharm environment package into another environment

【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
随机推荐
Variable used in lambda expression should be final or effectively final报错解决方案
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
买口罩(0-1背包)
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
Service
Import the pycharm environment package into another environment
way of thinking problem-solving skills
unity第一课
bzoj 5333 [Sdoi2018]荣誉称号
makefile记录
事务总结
6 states of a thread
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
Quectel EC20 4G module dial related
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决
Singleton DCL (double check the lock) full han mode and the hungry
Mysql实操
SIGINT, SIGKILL, SIGTERM signal difference, summary of various signals
es6 基础知识详解 变量 字符串 解构赋值 函数 对象 从入门到精通
Lottie系列四:使用建议