当前位置:网站首页>力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
2022-07-28 02:37:00 【ChaoYue_miku】
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
提示:
1 <= word.length, prefix.length <= 2000
word 和 prefix 仅由小写英文字母组成
insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-trie-prefix-tree
C++提交内容:
class Trie {
private:
vector<Trie*> children;
bool isEnd;
Trie* searchPrefix(string prefix) {
Trie* node = this;
for (char ch : prefix) {
ch -= 'a';
if (node->children[ch] == nullptr) {
return nullptr;
}
node = node->children[ch];
}
return node;
}
public:
Trie() : children(26), isEnd(false) {
}
void insert(string word) {
Trie* node = this;
for (char ch : word) {
ch -= 'a';
if (node->children[ch] == nullptr) {
node->children[ch] = new Trie();
}
node = node->children[ch];
}
node->isEnd = true;
}
bool search(string word) {
Trie* node = this->searchPrefix(word);
return node != nullptr && node->isEnd;
}
bool startsWith(string prefix) {
return this->searchPrefix(prefix) != nullptr;
}
};
/** * 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); */
边栏推荐
- Full of dry goods, hurry in!!! Easy to master functions in C language
- Kubernetes -- Introduction
- CAD创建组却没有组合在一起?
- 傅里叶级数
- My approval of OA project (meeting inquiry & meeting signature)
- 【stream】stream流基础知识
- Ci/cd from hardware programming to software platform
- More than 50 interviews have been summarized, and notes and detailed explanations have been taken from April to June (including core test sites and 6 large factories)
- 注意,这些地区不能参加7月NPDP考试
- Day 19 of leetcode
猜你喜欢

嵌入式分享合集22

关于权重衰退和丢弃法

"29 years old, general function test, how do I get five offers in a week?"

Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment

Data Lake: database data migration tool sqoop

工程电磁场复习基本知识点

嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践

CNN training cycle reconstruction - hyperparametric test | pytorch series (XXVIII)

JVM 内存布局详解,图文并茂,写得太好了!

【下载文件】uniapp开发小程序,下载文件并保存到本地
随机推荐
Superparameter adjustment and experiment - training depth neural network | pytorch series (26)
[2022 Niuke multi school 2 K link with bracket sequence I] bracket linear DP
JVM memory layout detailed, illustrated, well written!
mysql 随笔
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
Uniapp——拨打电话、发送短信
QFileDevice、QFile、QSaveFile、QTemporaryFile
Is the securities account given by qiniu safe? Can qiniu open an account and buy funds
Full of dry goods, hurry in!!! Easy to master functions in C language
【下载文件】uniapp开发小程序,下载文件并保存到本地
More than 50 interviews have been summarized, and notes and detailed explanations have been taken from April to June (including core test sites and 6 large factories)
Data Lake: flume, a massive log collection engine
The applet has obtained the total records and user locations in the database collection. How to use aggregate.geonear to arrange the longitude and latitude from near to far?
CAD creation group is not combined?
基于JSP&Servlet实现的众筹平台系统
树莓派开发继电器控制灯
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
数据湖(十七):Flink与Iceberg整合DataStream API操作
数字孪生技术驱动智能工厂减负赋能提升运维效益
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?