Trie 树模板

https://leetcode.com/problems/implement-trie-prefix-tree/

class TrieNode {
public:
char var;
bool isWord;
TrieNode *next[];
TrieNode() {
var = ;
this->isWord = false;
for(auto &c : next) c = NULL;
}
TrieNode(char c) {
var = c;
this->isWord = false;
for(auto &c : next) c = NULL;
}
}; 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 idx = a - 'a';
if(!p->next[idx]) p->next[idx] = new TrieNode();
p = p->next[idx];
}
p->isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode *p = root;
for(auto &a : word) {
int idx = a - 'a';
if(!p->next[idx]) return false;
p = p->next[idx];
}
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 idx = a - 'a';
if(!p->next[idx]) return false;
p = p->next[idx];
}
return true;
} 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) 、211. Add and Search Word - Data structure design

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

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

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

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

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

  4. 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 ...

  5. LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)

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

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

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

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

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

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

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

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

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

随机推荐

  1. 接口和JAVA设计模式

  2. 12 求1+2+...+n

    参考 http://www.cppblog.com/zengwei0771/archive/2012/04/28/173014.html 和 http://blog.csdn.net/shiren_b ...

  3. IOS 系统API---NSJSONSerialization四个枚举什么意思

    IOS 系统API---NSJSONSerialization四个枚举什么意思 NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NS ...

  4. 【转】深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  5. Some projects cannot be imported because they already exist in the workspace

    原文地址: Some projects cannot be imported because they already exist in the workspace - 浅尝辄止的博客 - 博客频道 ...

  6. 发现可高速缓存的 SSL 页面

    发现可高速缓存的 SSL 页面 技术描述: 缺省情况下,大部分 Web 浏览器都配置成会在使用期间高速缓存用户的页面. 这表示也会高速缓存 SSL 页面.不建议让 Web 浏览器保存任何 SSL 信息 ...

  7. Winform 数据验证

    http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx 总结:1. CancelEventArgs e ,调用e ...

  8. WCF异步

    WCF异步与否由客户端来决定 服务端接口: // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”.    [ServiceContract]   ...

  9. Extension Method[上篇]

    在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...

  10. python sort()和sorted()方法

    直接上代码: list_a=['a','c','z','E','T','C','b','A','Good','Tack'] list_b=['a','c','z','E','T','C','b','A ...