实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // 返回 true trie.search("app"); // 返回 false trie.startsWith("app"); // 返回 true trie.insert("app"); trie.search("app"); // 返回 true

说明:

  • 你可以假设所有的输入都是由小写字母 a-z 构成的。
  • 保证所有输入均为非空字符串。

前缀树的基础原理实现

class Trie {
public:
struct TrieNode
{
TrieNode() : isword(false), children(26, nullptr){}
~TrieNode()
{
for(TrieNode* child : children)
{
if(child)
delete child;
}
}
bool isword;
vector<TrieNode*> children;
};
/** Initialize your data structure here. */
Trie() : root(new TrieNode())
{
} TrieNode* root; const TrieNode* Find(const string &prefix) const
{
const TrieNode* newRoot = root;
for(const char c : prefix)
{
newRoot = newRoot ->children[c - 'a'];
if(newRoot == nullptr)
break;
}
return newRoot;
} /** Inserts a word into the trie. */
void insert(string word)
{
TrieNode* newRoot = root;
for(char c : word)
{
if(newRoot ->children[c - 'a'] == nullptr)
{
newRoot ->children[c - 'a'] = new TrieNode();
}
newRoot = newRoot ->children[c - 'a'];
}
newRoot ->isword = true;
} /** Returns if the word is in the trie. */
bool search(string word)
{
const TrieNode* tmp = Find(word);
return tmp != nullptr && tmp ->isword == true;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix)
{
return Find(prefix) != nullptr;
}
};

Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)的更多相关文章

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

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

  2. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

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

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

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

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

  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面试准备:Implement Trie (Prefix Tree)

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

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

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

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

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

  9. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

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

随机推荐

  1. 剑指offer——53字符流中第一个只出现一次的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  2. kafka相关业务必会操作命令整理

    参考:https://kafka.apache.org 服务相关命令 1.启动/停止zk > bin/zookeeper-server-start.sh config/zookeeper.pro ...

  3. leetcode.字符串.205同构字符串-Java

    1. 具体题目 给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不 ...

  4. [已解决]报错JSONDecodeError

    报错: 解决:

  5. hdu6315 /// 线段树区间更新

    题目大意: 给定n q 为序列的个数和操作的个数 给定n个数的序列b[]作为分母 初始全为0的序列a[]作为分子 两种操作 add l r 为a[]的l到r区间全部+1 query l r 为查询l到 ...

  6. SDL系列之 - 用SDL动态地画一个圆喽 && 设置背景色

    #include <SDL.h> #include <stdlib.h> #include <string.h> #include <math.h> # ...

  7. mongo数组修改器—$push、$ne、$addtoset、$pop、$pull

    这几个方法也很有意思 $push 像已有的数组末尾加入一个元素,要是元素不存在,就会创建一个新的元素,如果元素存在了,就会再添加一个一模一样的元素,会造成元素的重复,所以在使用的时候,要确保该元素不存 ...

  8. 从零开始搭建系统2.5——Apollo安装及配置

    参见https://github.com/ctripcorp/apollo/wiki/Quick-Start安装即可

  9. 用python输出1-100之间所有的质数

    # 1-100之间的质数 i = 2 while i <= 100: j = 2 flag = True while j < i: if i % j == 0: flag = False ...

  10. Spring Boot 2.X 实现文件上传(三)

    使用 SpringBoot 项目完成单个.多个文件的上传处理,并将上传的文件保存到指定目录下. 代码演示案例 所有的 HTML 页面文件 index.html <!DOCTYPE html> ...