class TrieNode {
public:
const static int NR_FANOUT = ;
TrieNode* child[NR_FANOUT];
int count;
// Initialize your data structure here.
TrieNode() {
for (int i=; i<NR_FANOUT; i++) {
child[i] = NULL;
}
count = ;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string s) {
insert(s, , root);
} // Returns if the word is in the trie.
bool search(string key) {
return search(key, , root) != NULL;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
return search_prefix(prefix, , root) != NULL;
}
private:
TrieNode* insert(string& s, int pos, TrieNode* current) {
int len = s.size();
if (pos > len) {
return NULL;
}
if (current == NULL) {
current = new TrieNode();
}
if (pos == len) {
current->count++;
return current;
}
int idx = s[pos] - 'a';
current->child[idx] = insert(s, pos + , current->child[idx]);
return current;
} TrieNode* search(string& s, int pos, TrieNode* current) {
if (current == NULL) {
return NULL;
}
int len = s.size();
if (pos > len) {
return NULL;
} else if (pos == len && current->count) {
return current;
}
return search(s, pos + , current->child[s[pos] - 'a']);
}
TrieNode* search_prefix(string& s, int pos, TrieNode* current) {
if (current == NULL) {
return NULL;
}
int len = s.size();
if (pos >= len) {
return current;
}
return search_prefix(s, pos + , current->child[s[pos] - 'a']);
}
private:
TrieNode* root;
};

mplement a trie with insertsearch, and startsWith methods.

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

LeetCode Tries 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 a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

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

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

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

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

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

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

  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 字典树)

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

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

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

  9. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

随机推荐

  1. git install

    wget -O git-master.zip https://codeload.github.com/git/git/zip/master unzip git-master.zip cd git-ma ...

  2. Dockerfile数据管理

    本文介绍Docker内部以及容器间的数据管理,在容器中管理数据主要有两种方式: 数据卷(Volumes) 挂载主机目录(Bind mounts) 数据卷 数据卷是一个可供一个或则多个目录使用的特殊目录 ...

  3. JVM中的对象生命周期

    在JVM运行空间中,对象的整个生命周期大致可以分为七个阶段:创建阶段(Creation).应用阶段(Using).不可视阶段(Invisible).不可到达阶段( Unreachable).可收集阶段 ...

  4. 为什么有监听socket和连接socket,为什么产生两个socket

    为什么有监听socket和连接socket,为什么产生两个socket 先看一半的socket建立连接的双方的过程: 客户端: socket()---->创建出 active_socket_fd ...

  5. 如何让IE 低版本下支持 css3属性

    依赖源  该文件为  ie-css3.htc    (特别提示.htc为二进制文件,只会在ie中识别,让IE浏览器支持CSS3的一些属性) 以下为依赖文件源码 通过源码我们可以看到 该文件在一定程度上 ...

  6. (转)Mysql技术内幕InnoDB存储引擎-表&索引算法和锁

    表 原文:http://yingminxing.com/mysql%E6%8A%80%E6%9C%AF%E5%86%85%E5%B9%95innodb%E5%AD%98%E5%82%A8%E5%BC% ...

  7. Linux的帮助文档命令

    Linux的帮助文档命令 1.man page man是manual(操作手册)的简写,使用方式: man [指令] man date 在显示的内容中查找内容: / + 搜索你的关键字 上下左右键来查 ...

  8. sql_auoload_regiester() 解释(转载)

    在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: 运行index.P ...

  9. JVM-垃圾收集算法、垃圾收集器、内存分配和收集策略

    对象已死么? 判断一个对象是否存活一般有两种方式: 1.引用计数算法:每个对象都有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1.计数为0时可以回收. 2.可达性分析算法(Reachab ...

  10. 【jQuery源码】preFilter

    preFilter: { "ATTR": function( match ) { //属性名解码 match[1] = match[1].replace( runescape, f ...