【LeetCode 208】Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
思路:
构建一个简单的字典树,要求实现插入、查找、查找前缀三个操作。这里使用map实现对子树的构建(一方面方便查找,另一方面是懒 - -!),但是效率并不是太高,LeetCode上提交后跑了147ms,不是很理想,不过不影响对字典树这种树形结构的理解。
class TrieNode {
public:
// Initialize your data structure here.
TrieNode(char c):ch(c), isEnd(false){}
map<char, TrieNode *> childNode;
char ch;
bool isEnd;
};
class Trie {
public:
Trie() {
root = new TrieNode('#');
}
// Inserts a word into the trie.
void insert(string s) {
TrieNode *t = root;
//从root遍历待插入字符串,若c存在则继续向下遍历,反之则新建一个分支
for(int i = ; i < s.length(); i++)
{
char c = s[i];
map<char, TrieNode*>::iterator it = t->childNode.find(c);
if(it == t->childNode.end())
{
TrieNode *newNode = new TrieNode(c);
t->childNode.insert(make_pair(c, newNode));
t = newNode;
}
else
{
t = t->childNode[c];
}
}
t->isEnd = true;
}
// Returns if the word is in the trie.
bool search(string key) {
TrieNode *s = root;
//从root遍历待查找字符串,若c存在则向下遍历,反之则不存在
for(int i = ; i < key.length(); i++)
{
char c = key[i];
map<char, TrieNode*>::iterator it = s->childNode.find(c);
if(it == s->childNode.end())
return false;
else
s = s->childNode[c];
}
//返回最后一个结点的状态,判断是否为一个word
return s->isEnd;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *s = root;
//与search一致
for(int i = ; i < prefix.length(); i++)
{
char c = prefix[i];
map<char, TrieNode*>::iterator it = s->childNode.find(c);
if(it == s->childNode.end())
return false;
else
s = s->childNode[c];
}
//只要找到即返回true
return true;
}
private:
TrieNode* root;
};
【LeetCode 208】Implement Trie (Prefix Tree)的更多相关文章
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- 【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)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- 字典树(查找树) 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) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 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 ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- [SQL Server 系] -- 模糊查询
SQL Server中的通配符有下面四种 通配符 说明 % 包含零个或多个字符的任意字符串 _(下划线) 任意单个字符 [ ] 任意在指定范围或集合中的单个字符 [^ ] 任意不在指定范围或集合中的单 ...
- Bash 小知识点
变量定义的时候=两边不能有空格,例如: a='Hello World' 如果变量和其它字符相连,可以用{}把变量引起来,这样就可以和相连的字符隔离 除了在变量赋值和在FOR循环语句头中,BASH中的变 ...
- java.lang.NoClassDefFoundError: JspException
在打开jsp页面的时候报错java.lang.NoClassDefFoundError: JspException,如下所示: 原因和解决方案: 原因是由于包不全 把该导的包导进去,在上面的例子就是由 ...
- Openfire 代码部署报错: Variable references non-existent resource:${workspace_loc:openfire_src}
Variable references non-existent resource:${workspace_loc:openfire_src} -DopenfireHome=“${workspace_ ...
- lintcode :搜索旋转排序数组
题目 搜索旋转排序数组 假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2).给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引 ...
- 自旋锁spin_lock和raw_spin_lock
自旋锁spin_lock和raw_spin_lock Linux内核spin_lock.spin_lock_irq 和 spin_lock_irqsave 分析 http://blog.csdn.ne ...
- HDU5087——Revenge of LIS II(BestCoder Round #16)
Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...
- Objective-c CoreData
#import "AppDelegate.h" #import "Person.h" @implementation AppDelegate @synthesi ...
- 【C#设计模式——创建型模式】简单工场模式
进入码农行列也有一年半载了,仍然感觉自己混混沌沌,无所事事,无所作为,,,想想都下气,下气归下气,仍要奋起潜行,像愤怒的小鸟一边又一遍的冲向猪头也好,像蜗牛一样往前蹭也罢,总之要有蚂蚁啃骨头的精神!! ...
- 自定义View(6)paint设置图图层重叠时的显示方式,包含清空canvas
Paint.setXfermode 这个函数设置两个图层相交时的模式 在已有的图层上绘图将会在其上面添加一层新的图层. 如果新的图层是完全不透明的,那么它将完全遮挡住下面的图层,而setXfermod ...