当前位置:网站首页>Leetcode 208. implement trie (prefix tree) (2022.07.27)
Leetcode 208. implement trie (prefix tree) (2022.07.27)
2022-07-28 03:22:00 【ChaoYue_ miku】
Trie( It sounds like “try”) Or say Prefix tree It's a tree data structure , Keys for efficient storage and retrieval of string datasets . This data structure has quite a number of application scenarios , For example, automatic completion and spelling check .
Please realize Trie class :
Trie() Initialize the prefix tree object .
void insert(String word) Insert a string into the forward tree word .
boolean search(String word) If the string word In the prefix tree , return true( namely , Has been inserted before retrieval ); otherwise , return false .
boolean startsWith(String prefix) If a string has been inserted before word One of the prefixes of is prefix , return true ; otherwise , return false .
Example :
Input
[“Trie”, “insert”, “search”, “search”, “startsWith”, “insert”, “search”]
[[], [“apple”], [“apple”], [“app”], [“app”], [“app”], [“app”]]
Output
[null, null, true, false, true, null, true]
explain
Trie trie = new Trie();
trie.insert(“apple”);
trie.search(“apple”); // return True
trie.search(“app”); // return False
trie.startsWith(“app”); // return True
trie.insert(“app”);
trie.search(“app”); // return True
Tips :
1 <= word.length, prefix.length <= 2000
word and prefix It's only made up of lowercase letters
insert、search and startsWith Call the number A total of No more than 3 * 104 Time
source : Power button (LeetCode)
link :https://leetcode.cn/problems/implement-trie-prefix-tree
C++ Submission :
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); */
边栏推荐
猜你喜欢

MySQL事务的ACID特性及并发问题实例分析

Softek Barcode Reader 9.1.5

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

【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法

C#WinForm开发:如何将图片添加到项目资源文件(Resources)中

Redis内存回收

Comprehensive comparative study of image denoising

The test post changes jobs repeatedly, jumping and jumping, and then it disappears

Leaf recognition, color feature extraction, defect detection, etc

Which of the four solutions of distributed session do you think is the best?
随机推荐
C -- switch case statement
关于湖北文理学院平衡信标组的疑问回应
Exness: Japanese prices rose and incomes fell, with the pound / yen breaking 165
IO analog serial port of stm32
Which of the four solutions of distributed session do you think is the best?
Web server
How to reinstall win11 system with one click
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
VI command details
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
C # set TextBox control not editable
Scheme sharing | experts gather to jointly explore accent AI speech recognition
嵌入式数据库--SQLite
53. Maximum Subarray最大子数组和
Raspberry pie development relay control lamp
QML使用Layout布局时出现大量<Unknown File>: QML QQuickLayoutAttached: Binding loop detected for property循环绑定警告
关于权重衰退和丢弃法
上位机与MES对接的几种方式
Redis内存回收
20 soul chicken soup beautiful sentences, sentence by sentence warm heart!