Implement a trie with insertsearch, 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)的更多相关文章

  1. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  2. LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))

    Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...

  3. 【LeetCode】208. Implement Trie (Prefix Tree)

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

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

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

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

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

  6. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

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

  7. 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  ...

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

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

  9. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

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

随机推荐

  1. 套题T6

    过节(festival.cpp/c/pas) Dxy帮老师们过教师节的时候需要购买礼物.货架上有n 种礼物,每种礼物有不同的个数.每种礼物有价值和花费两种属性,帮他算出最大可以得到的价值.M是带的钱数 ...

  2. hdu 3972 1 M possible

    一般做法: 显然的超内存 #include<stdio.h> #include<algorithm> using namespace std; ],ans[]; int mai ...

  3. go语言入门

    Go语言最主要的特性: 自动垃圾回收 更丰富的内置类型 函数多返回值 错误处理 匿名函数和闭包 类型和接口 并发编程 反射 语言交互性 1.2.4 错误处理Go语言引入了3个关键字用 ...

  4. 漫谈C语言及如何学习C语言

    抄自:http://my.oschina.net/apeng/blog/137911 目录:[ - ] 为什么要学习C语言? C语言学习方法 1,参考书籍 2,动手实验环境搭建 3,网络资源 附录 一 ...

  5. 手动安装UniDAC的过程

    每次安装都不一样,而且有时候挺麻烦的,所以记下来.我这次安装过程是这样的: 0. 修改Source\Delphi7\Make.bat,填入正确的IdeDir 1.找到source\delphi7目录, ...

  6. C#实现的ReplaceFirst和ReplaceLast

    原文:C#实现的ReplaceFirst和ReplaceLast ReplaceFirst: public static string ReplaceFirst(string input, strin ...

  7. Intellij IDEA创建javaWeb以及Servlet简单实现

    1.创建web工程 File --> New --> Project...

  8. 机器学习 —— 概率图模型(学习:CRF与MRF)

    在概率图模型中,有一类很重要的模型称为条件随机场.这种模型广泛的应用于标签—样本(特征)对应问题.与MRF不同,CRF计算的是“条件概率”.故其表达式与MRF在分母上是不一样的. 如图所示,CRF只对 ...

  9. Android 自定义控件-TextView

    很多时候系统自带的View满足不了设计的要求,就需要自定义View控件.自定义View首先要实现一个继承自View的类.添加类的构造方法,override父类的方法,如onDraw,(onMeasur ...

  10. Unity获取方法的参数和方法名称

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { Console.Write ...