【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 ...
随机推荐
- 找啊找啊找GF
P1013 找啊找啊找GF 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 MM七夕模拟赛 描述 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手, ...
- 缓存初解(四)---Ibatis的缓存配置+Ehcache
项目完结,整理一些技术方面的相关收获. 已经记不得EhCacheController这个实现类最早来自于那里了,总之稍加修改后非常有效果,大家就这么用了,感谢最初开源的那位兄弟.这里,主要是做个记录, ...
- PL/SQL-Thread creation error:存储空间不足,无法处理此命令
PL/SQL中执行SQL语句,提示“Thread creation error:存储空间不足,无法处理此命令”.查找了解决方案,如下: 1. 单击开始,然后单击运行. 2. 键入 regedit,然后 ...
- 224. Basic Calculator
题目: Implement a basic calculator to evaluate a simple expression string. The expression string may c ...
- Android关于实现EditText中加多行下划线的的一种方法
1. 重写EditText public class LinedEditText extends EditText { private Paint linePaint; private float m ...
- 12个目标跟踪方面的资料12 Tracking
Goal Tracking Template - FEMA.gov Goal Tracking Template Set a weekly or biweekly deadline to report ...
- C#画图解决闪烁问题
导致画面闪烁的关键原因分析: 一.绘制窗口由于大小位置状态改变进行重绘操作时,绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面重新刷新一次以维持窗口正常显示 ...
- Codeforces Round #239 (Div. 2)
做了三个题,先贴一下代码...终于涨分了 A. Line to Cashier 水题 #include <iostream> #include <cstdio> #includ ...
- 函数buf_page_hash_get_low
/******************************************************************//** Returns the control block of ...
- 原创-兼容IE8的placeholder
!function (o) { o.fn.extend({ PlaceHolder: function () { var _isEmpty = function (val) { return (val ...