LeetCode Tries Prefix Tree
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 insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
LeetCode Tries Prefix Tree的更多相关文章
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
随机推荐
- Swift 里字符串(九)UTF16View
即以 UTF16 编码的格式来查看字符串. UTF16View 是一个结构体 @_fixed_layout public struct UTF16View { @usableFromInline in ...
- Python小白学习之路(四)——第一次练习题
写在前面: 今天下雪了呢!连着两天都没有更新学习记录. 我没有偷懒呢.做了一天的练习题,昨天学的内容还没总结完,太累了就回去睡觉了 连续一周早起,强大的内心也无法支撑我疲惫的身体 今天早起做了整理.加 ...
- POJ 1129
#include<iostream> #include<stdio.h> #include<string> #define MAXN 60 using namesp ...
- "|",“.”,"include"在jade中的用法(原样输出)
我们知道, body --><body> abc --><abc></abc> --></body> 如果我们想直接输入内容怎么办呢: ...
- Day1 Excel基本知识
1. 数据分析过程 2. 2016增强版Excel 特性 科学管理,科学决策的工具 精细化,量化分析的工具 3. Excel 学什么? 计算机解决问题的思维(抽象) 数据分析的思路与方法 掌握技巧和方 ...
- ThreadLocal的实现机制
TLS(Thread Local Storage)通过分配更多内存来解决多线程对临界资源访问的互斥问题,即每个线程均自己的临界资源对象, 这样也就不会发生访问冲突,也不需要锁机制控制,比较典型的以空间 ...
- 优化 JS 条件语句的 5 个技巧
优化 JS 条件语句的 5 个技巧 原创: 前端大全 前端大全 昨天 (给前端大全加星标,提升前端技能) 编译:伯乐在线/Mr.Dcheng http://blog.jobbole.com/11467 ...
- Maven-pom.xml文件报错 Plugin execution not covered by lifecycle configuration
问题: Eclipse中新导入的项目pom.xml文件报错: Plugin execution not covered by lifecycle configuration: org.jacoco:j ...
- getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- Attr.checkId()方法
1.符号sym是TYP02 举个例子,如下: package bazola; class Point { // ... } class Tree<A> { class AttrVisito ...