当前位置:网站首页>力扣(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); */
边栏推荐
- Redis群集
- 【stream】并行流与顺序流
- Confusion matrix in CNN | pytorch series (XXIII)
- 工程地质实习-工程地质 题集
- 阿里云国际版邮件服务套餐购买流程
- CAD creation group is not combined?
- Data Lake: flume, a massive log collection engine
- Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
- [QNX hypervisor 2.2 user manual]9.10 pass
- 线程基础
猜你喜欢

Using pytorch's tensorboard visual deep learning indicators | pytorch series (25)

嵌入式分享合集22

CAD creation group is not combined?

基于JSP&Servlet实现的众筹平台系统

Promise object

RBD块存储设备的扩容以及缩容操作(六)

Day 19 of leetcode

图像去噪综合比较研究

Data center construction (III): introduction to data center architecture

The test post changes jobs repeatedly, jumping and jumping, and then it disappears
随机推荐
STM32之IO模拟串口篇
vba批量读取sql的create文来创建表
OA项目之我的审批(会议查询&会议签字)
Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
一次跨域问题的记录
[acwing 1064 little king] shaped pressure DP
Uniapp——拨打电话、发送短信
ELS keyboard information
数据湖(十七):Flink与Iceberg整合DataStream API操作
汇总了50多场面试,4-6月面经笔记和详解(含核心考点及6家大厂)
Games101 review: ray tracing
NPDP考生!7月31号考试要求在这里看!
数据湖:海量日志采集引擎Flume
Day 8 of DL
MySQL essay
Ah Han's story
Comprehensive case
Data Lake: flume, a massive log collection engine
QT topic 1: implementing a simple calculator