Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

一个字母代表一个子树,因此为26叉树,end标记表示是否存在以该字母为结尾的字符串。

class TrieNode {
public:
TrieNode* children[];
bool end;
// Initialize your data structure here.
TrieNode() {
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size())
{
if(curNode->end == true)
// exist
return;
else
// insert
curNode->end = true;
}
else
{
while(i < word.size())
{
curNode->children[word[i]-'a'] = new TrieNode();
curNode = curNode->children[word[i]-'a'];
i ++;
}
curNode->end = true;
}
} // Returns if the word is in the trie.
bool search(string word) {
int i = ;
TrieNode* curNode = root;
while(i < word.size() && curNode->children[word[i]-'a'] != NULL)
{
curNode = curNode->children[word[i]-'a'];
i ++;
}
if(i == word.size() && curNode->end == true)
// curNode must be leaf
return true;
else
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i = ;
TrieNode* curNode = root;
while(i < prefix.size() && curNode->children[prefix[i]-'a'] != NULL)
{
curNode = curNode->children[prefix[i]-'a'];
i ++;
}
if(i == prefix.size())
// curNode might be left or not
return true;
else
return false;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

【LeetCode】208. Implement Trie (Prefix Tree)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  8. Java for LeetCode 208 Implement Trie (Prefix Tree)

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

  9. leetcode@ [208] Implement Trie (Prefix Tree)

    Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...

随机推荐

  1. 理解SVG图片标签的viewport、viewBox、preserveAspectRatio缩放

    一.viewport 表示SVG可见区域的大小,或者可以想象成舞台大小,画布大小. <svg width="></svg> 上面的SVG代码定义了一个视区,宽500单 ...

  2. laravel 开启sql调试

    打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容 public function boot() { //sql调试 $sql_debug = ...

  3. 转:fastText原理及实践(达观数据王江)

    http://www.52nlp.cn/fasttext 1条回复 本文首先会介绍一些预备知识,比如softmax.ngram等,然后简单介绍word2vec原理,之后来讲解fastText的原理,并 ...

  4. Java 解决 servlet 接收参数中文乱码问题

    方法一: 接收到的参数进行如下操作[不建议]: String tmp = new String(type.getBytes("iso-8859-1"), "utf-8&q ...

  5. css的overflow属性

    原文:https://www.jianshu.com/p/67b536fc67c1 ------------------------------------------- 事实上我挺长一段时间都没弄清 ...

  6. form表单的reset

    $(':input','#myform') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .rem ...

  7. insta经典滤镜下载

    好不容易找到的Insta的经典滤镜源码,贴出来帮大家学习. // // IFImageFilter.m // InstaFilters // // Created by Di Wu on 2/28/1 ...

  8. PDF文档输出

    参考:http://ajava.org/article-685-1.html Flexpaper> 参数说明: Flexpaper参数说明SwfFile (String) 需要使用Flexpap ...

  9. C#.NET常见问题(FAQ)-索引器indexer有什么用

    索引器就是名值对,你声明一个名值对的类,就可以给数组的指定元素赋值,也可以很方面的访问到指定元素值对应的下标(个人认为完全可以自己写一个类似的list来实现,没有必要多此一举学一个额外的方法)   复 ...

  10. gdbserver 移植与多线程调试

    在嵌入式linux平台使用gdb调试进行远程调试需要安装gdbserver,gdbserver工作在目标板上,通过串口或者网线与主机上的gdb互联实现远程调试. Gdbserver需要根据不同的嵌入式 ...