设计服务网站wordpress 如何安装教程
设计服务网站,wordpress 如何安装教程,网络舆情分析师怎么考,wordpress抓取淘宝价格208. 实现 Trie (前缀树) - 力扣#xff08;LeetCode#xff09;
总结#xff1a;
Trie#xff0c;又称前缀树或字典树#xff0c;是一棵有根树#xff0c;其每个节点包含以下字段#xff1a;
指向子节点的指针数组 children。对于本题而言#xff0c;数组长度为 26…208. 实现 Trie (前缀树) - 力扣LeetCode
总结
Trie又称前缀树或字典树是一棵有根树其每个节点包含以下字段
指向子节点的指针数组 children。对于本题而言数组长度为 26即小写英文字母的数量。此时 children[0] 对应小写字母 achildren[1]对应小写字母 b…children[25]对应小写字母 z。 布尔字段 isEnd\textit{isEnd}isEnd表示该节点是否为字符串的结尾。解释来源leetcode官方。用来实现查早字符串是否存在以及是否作为字串出现过。需要注意的地方是每个Trie指针都是指向一整个对象包括了vector和一个bool而不是指向了vector中的某一个对象。
代码
class Trie {
public:vectorTrie* 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;}Trie():children(26,nullptr),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;}
};
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/90218.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!