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. 为什么static数据成员一定要在类外初始化?(转)

    1.避免重复定义和初始化 <<c++ primer>>说在类外定义和初始化是保证static成员变量只被定义一次的好方法. 但,为什么static const int就可以在类 ...

  2. Android Migrate Android Code

    近期在更改程序架构的时候,用引用Library的方式实现程序的共享化过程中发现R.id.xx的文件不能够在  switch cases  中使用 看代码提示,如下信息: As of ADT 14, r ...

  3. centos:添加用户

    初步进入centos学习,配置用户 1,创建用户 创建用户 hadoop [root@master spark]# useradd hadoop [root@master spark]# 2,创建密码 ...

  4. Git 以分支的方式同时管理多个项目

    你是否遇到过这样的问题: 你的客户在你们这边做了N个项目,而项目之间又存在着某些业务关联(数据库访问等) 之前你可能是这样处理的,为客户的每个项目创建单独的Git版本 PC项目 手机项目 微信项目 其 ...

  5. w3cscholl的在线代码编辑工具

    https://www.w3cschool.cn/tryrun/runcode?lang=c

  6. ZOJ 3456 Traveler Nobita 最小生成树

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  7. 数据驱动测试二:使用TestNG和CSV文件进行数据驱动

    转载:https://blog.csdn.net/heart_1014/article/details/52013173 使用@DataProvider注解定义当前方法中的返回对象CSV文件(存放测试 ...

  8. (纪录片)数学的故事 The Story of Maths (2008)

    简介: 导演: Robin Dashwood编剧: Marcus du Sautoy主演: Marcus du Sautoy类型: 纪录片官方网站: www.bbc.co.uk/programmes/ ...

  9. SDE注册版本失败,仅支持一个空间列

    如果直接编辑SDE要素类与要素可以不需要版本,使用默认版本,如果要让用户通过界面编辑,即使用开启编辑.保存编辑和停止编辑,就需要注册为版本,而在注册版本弹出如下错误: 正如错误所说,一个要素类或shp ...

  10. android中RecyclerView控件实现长按弹出PopupMenu菜单功能

    之前写过一篇文章:android中实现简单的聊天功能 现在是在之前功能的基础上,添加一个长按聊天记录,删除对应聊天记录的功能 RecyclerView控件,没有对应的长按事件,我们需要自己手工添加,修 ...