Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

一个字母代表一个子树,因此为26叉树,end标记表示是否存在以该字母为结尾的字符串。

class TrieNode {
public:
TrieNode* children[];
bool end;
// Initialize your data structure here.
TrieNode() {
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size())
{
if(curNode->end == true)
// exist
return;
else
// insert
curNode->end = true;
}
else
{
while(i < word.size())
{
curNode->children[word[i]-'a'] = new TrieNode();
curNode = curNode->children[word[i]-'a'];
i ++;
}
curNode->end = true;
}
} // Returns if the word is in the trie.
bool search(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size() && curNode->end == true)
// curNode must be leaf
return true;
else
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i = ;
TrieNode* curNode = root;
while(i < prefix.size() && curNode->children[prefix[i]-'a'] != NULL)
{
curNode = curNode->children[prefix[i]-'a'];
i ++;
}
if(i == prefix.size())
// curNode might be left or not
return true;
else
return false;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

【LeetCode】208. Implement Trie (Prefix Tree)的更多相关文章

  1. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  2. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  3. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  4. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  5. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  6. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  7. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  8. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  9. leetcode@ [208] Implement Trie (Prefix Tree)

    Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...

随机推荐

  1. windows 使用 xxfpm 解决 php-cgi 进程自动关闭

    windows 下 php-cgi 进程处理一定数量的访问后,就会自动关闭,由于没办法直接让 php-cgi 进程支持更多的访问数量,所以只能启动多个进程来满足需求. xxfpm 是一个可执行程序,它 ...

  2. 转:pytorch版的bilstm+crf实现sequence label

    http://blog.csdn.net/appleml/article/details/78664824 在理解CRF的时候费了一些功夫,将一些难以理解的地方稍微做了下标注,隔三差五看看加强记忆, ...

  3. 文本相似度-BM25算法

    BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms app ...

  4. 使用 Edit + MASM 5.0 编译器 + Linker 连接器

    其实这种方式是很简单的,只是很麻烦,因为简单而且麻烦, 所以我采用尽可能的将截图传上来,然后稍加注解的方式进行介绍, 软件准备: 需要 MASM 5.0 或者以上的汇编编译器 首先,是要编辑汇编源代码 ...

  5. 7.4 Javascript:表单验证-揭开正則表達式的面纱

    用元字符匹配对应的字符类型 创建正則表達式有点像创建字符串字面量,仅仅只是正則表達式出如今一对"/"里 正則表達式中会用到一级元字符.用于连接字母与数字 "." ...

  6. oauth2-server-php-docs 集成3

    Yii集成 对于Yii集成,请通过Filsh查看Yii OAuth2服务器资源库 CakePHP的 有关CakePHP中集成的这个库的示例,请参阅Qsoomro CakePHP OAuth2 Demo ...

  7. Unity3d -> Xcode 多个渠道版本发布文件合并

    第一步: Users/xxx/.jenkins/jobs/projectname/workspace/build/iOS_iphone 把这里面所有文件拷贝到生成的xcode 工程下的Data目录 如 ...

  8. 牛客网-《剑指offer》-矩形覆盖

    题目:http://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6 C++ class Solution { public: in ...

  9. vsphere 处理NUMA

    vsphere 4.1 之前: cpu调度会将一个VM的分配给一个home node,整个vm被看做一个NUMA client. 如果VM的vCPU数量超过一个NUMA node的可用数量,则不被看做 ...

  10. Atlas:ERROR 1105 (HY000): #07000Proxy Warning - IP Forbidden

    1:遇到一个奇怪的问题 Atlas的管理接口正常 添加一个client之后save config mysql -uroot -p -P1234 -h127.0.0.1 报错了:ERROR 1105 ( ...