Trie 树实现

Implement a trie with insert, search, and startsWith methods.

class TrieNode {
public:
// Initialize your data structure here. TrieNode *child[];
bool isWord;
TrieNode():isWord(false){
for(auto &a : child)
a = nullptr;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(!p->child[i]) return false;
p = p->child[i];
}
return p->isWord;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* p = root;
for(auto &a : prefix){
int i = a - 'a';
if(!p->child[i]) return false;
p = p->child[i];
}
return true;
} private:
TrieNode* root;
};

参看:http://www.cnblogs.com/grandyang/p/4491665.html

 

Implement Trie (Prefix Tree)的更多相关文章

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

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

  2. 【LeetCode】208. Implement Trie (Prefix Tree)

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

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

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

  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. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

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

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

  7. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

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

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

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

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

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

  10. Implement Trie (Prefix Tree) 解答

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

随机推荐

  1. app上传到App Store的快捷方法及步骤

    跳过证书的申请及配置概要文件的设置, 现在根据已有的配置概要文件及发布证书开始: 1.先在Xcode上的PROJECT和TARGETS->Build Setting->Code Signi ...

  2. 设计模式(3)--抽象工厂模式(Absrtact Factory Pattern)

    定义 抽象工厂模式的实质就是提供接口来创建一系列相关或独立的对象而不指定这些对象的具体类. 理解 在软件系统中,经常面临着"一系列相互依赖的对象"的创建工作:同时由于需求的变化,往 ...

  3. list内容按长度等分

    这里需要导入 from more_itertools import chunked chunked(iterable, n) 将一个可迭代对象等分成n个list,第n个list的长度可能小于之前的. ...

  4. php类的__get和__set方法

    (1)这两个方法是自动调用的 (2)这两个方法只有在成员变量是private的时候才会自己调用 testclass.php <?php class testclass { private $va ...

  5. python 中的高级函数sorted()

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  6. SharePoint SC "Audit Settings"功能与CSOM的对应

    博客地址:http://blog.csdn.net/FoxDave SharePoint网站集中有个关于审计的功能:"Site collection audit settings&quo ...

  7. hashmap 读取

    hashTable hashSet 都差不多 以hashmap为例,底层是一个散列表 数组,然后数组存出一个entry对象,对象中有两个泛型属性,一个可以指向自身类型的引用,这样就可以在每一个数组的位 ...

  8. 第五章 搭建S3C6410开发板的测试环境

    在PC上可以开发Linux驱动,重新编译成ARM架构的Linux驱动模块,但最后还是要在开发板上进行测试.目前最流行的是基于三星S3C6410 ARM11架构的开发板,很多厂商在其基础上进行了扩展,开 ...

  9. mySQL基本操作学习笔记(一)

                                                                                                        ...

  10. Android:Layout_weight的深刻理解

    最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...