当前位置:网站首页>208. implement trie (prefix tree) - attach detailed notes
208. implement trie (prefix tree) - attach detailed notes
2022-06-30 01:40:00 【The_ Dan】
class Trie {
private:
vector<Trie*> vecTree; // Prefix number of child nodes , Generally speaking, there are 26 individual
bool isEnd; // If the point is the last node of a string , Then the value is true, Otherwise false
Trie* searchPrefix(string prefix){
// because search and startsWith There is commonality , Sum it up into a function
Trie* node = this;
for(char c : prefix){
// Compare with the prefix tree layer by layer in the string
c = c - 'a';
if(node -> vecTree[c] == nullptr){
// If it doesn't exist , That is, the prefix does not exist , Returns an empty
return nullptr;
}
// Enter the corresponding node of the next layer
node = node -> vecTree[c];
}
// All the comparisons are finished , Indeed, one-to-one correspondence , Returns the last prefix node
return node;
}
public:
Trie() : vecTree(26), isEnd(false) {
} // By default, each node has 26 Child node , And not the last node
// Insert the function
void insert(string word) {
// In general, it is related to searchPrefix The same function
// But when there is no prefix node , Here is a new node
Trie* node = this;
for(char c : word){
c = c - 'a';
if(node -> vecTree[c] == nullptr){
node -> vecTree[c] = new Trie();
}
node = node -> vecTree[c];
}
// This is the end of the string , So make a mark , This indicates that a complete string can be formed
node -> isEnd = true; // There is a string marked here
}
// Lookup function , Not only through searchPrefix You can find the prefix , And this node is the last node of the string
// The last node here only indicates that this node can form a complete string , The point after this node will also exist isEnd = true
bool search(string word) {
Trie* node = searchPrefix(word);
return node != nullptr && node -> isEnd;
}
// Find prefix function , Compared with the previous function , Just know that the prefix exists
bool startsWith(string prefix) {
return searchPrefix(prefix) != nullptr;
}
};
Accepted
15/15 cases passed (64 ms)
Your runtime beats 34.91 % of cpp submissions
Your memory usage beats 26.27 % of cpp submissions (47.2 MB)
边栏推荐
- The (3n+1) conjecture that C language kills people without paying for their lives
- 魔百盒CM201-2-CH-Hi3798MV300-300H-EMMC和NAND_红外蓝牙语音_通刷固件包
- (1) Basic learning - figure out the difference between pin, pad, port, IO and net
- C语言 写出这个数
- C language I want to pass
- The first technology podcast month will begin soon
- C语言 素数对猜想
- Varnish 基础概览2
- OpenCV和Image之间的转换(亲测有效)
- Is the processor the main factor in buying a mobile phone?
猜你喜欢

手势数字启蒙学习机

C language I want to pass
![【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类](/img/5f/b23b64eed7f28ffd92c122b6859e2d.png)
【图神经网络】图分类学习研究综述[2]:基于图神经网络的图分类
![[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars](/img/8f/c0cc1c8d19060a60d92c0d72f8b93d.png)
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars

Embedded exit (review and release)

C语言 数素数

C语言 换个格式输出整数

TP-LINK configure WiFi authentication method for wireless Internet SMS

Write this number in C

Is the processor the main factor in buying a mobile phone?
随机推荐
Internal class usage scenarios, syntax and principle explanations
C language number prime
JS reverse request parameter encryption:
MySQL monitoring 2
Varnish foundation overview 3
A summary of the quantification of deep network model
Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
Mysql 监控6
Sentinel source code analysis Part 8 - core process - sphu Entry current limiting execution
关于c语言main函数中int argc,char **argv的理解
Cub school learning: manual query and ADC in-depth use
想转行,但不知道自己要做什么工作比较好?
(1) Basic learning - figure out the difference between pin, pad, port, IO and net
Mysql 监控5
Gesture digital enlightenment learning machine
[machine learning Q & A] data sampling and model verification methods, hyperparametric optimization, over fitting and under fitting problems
Cookie encryption 10
ctfshow 大赛原题 680-695
Varnish 基础概览2
Write this number in C