题解:这里主要复习面向对象的方法
- Trie,又称前缀树或字典树,是一棵有根树,其每个节点包含以下字段:
- 指向子节点的指针数组 children。对于本题而言,数组长度为 26,即小写英文字母的数量。此时 children[0] 对应小写字母 a,children[1] 对应小写字母 b,…,children[25] 对应小写字母 z。
- 布尔字段 isEnd,表示该节点是否为字符串的结尾。
官网题解
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);
*/