【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 ...
随机推荐
- apache配置优化
最近参加了很多面试,多多少少有点小感悟,可以说观念转变了不少,特别是对于作为一个开发人员的定位,原来只是认为开发人员就只需要写好代码就行了,所以只需要有数据结构,算法,设计模式,重构方面的知识就行了. ...
- 专家谈国产CPU最新发展态势:需强化标准建设(很全面)
一.国产CPU发展现状与成就 国内已开启多技术路线并行的CPU技术产业新格局.在国家科技重大专项和国家级集成电路产业投资资金的推动之下,我国CPU产品技术研发已进入多技术路线同步推进的高速发展阶段,并 ...
- Splunk作为日志分析平台与Ossec进行联动
背景: Ossec安装后用了一段时间的analogi作为ossec的报警信息显示平台,但是查看报警分类信息. 以及相关图标展示等方面总有那么一点点的差强人意,难以分析.因此使用逼格高一点的splunk ...
- Android Handler传值方式
前面介绍handler的时候,也用到过几种传值方式,今天来总结一下,并且重点说一下bundle方式,代码如下: package com.handlerThread; import android.ap ...
- ios7 webapp touch bug
// ios7 touchstart bug if(navigator.userAgent.indexOf("iPhone OS 7") != -1){ var startX = ...
- IIS发布报错
IIS发布报错一般原因 ISAPI和CGI限制作为IIS与ASP.NET的连接桥梁
- iOS开发:UINavigationController常用操作
NavigationController常用操作: 更改bar的背景颜色:self.navigationController?.navigationBar.barTintColor =UIColor. ...
- 终极解决方案:windows10资源管理器假死
想解决这个问题有以下几点: 1,不要相信所谓的powershell里面输命令可以重装系统自带应用从而解决假死问题! 2,不要相信烂大街的“自启动User Manager”服务可以解决假死问题! 3,不 ...
- max-height,min-height在IE下不支持的解决方法
max-height,min-height在IE下不支持的解决方法 max-width:160px; max-height:160px; _width:expression(this.width &g ...
- 封装Log工具类
public class LogUtil { public static final int VERBOSE = 1; public static final int DEBUG = 2; publi ...