实现一个 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. Django框架(三)—— orm增删改查、Django生命周期

    目录 orm增删改查.Django生命周期 一.orm介绍 二.orm增删改字段 三.Django生命周期 orm增删改查.Django生命周期 一.orm介绍 1.什么是orm ORM即Object ...

  2. Linux操作基础

    摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...

  3. 53-Ubuntu-打包压缩-3-gzip压缩和解压缩介绍

    gzip tar与gzip命令结合可以实现文件打包和压缩. tar只负责打包文件,但不负责压缩. 用gzip压缩tar打包后的文件,其扩展名一般为xxx.tar.gz. 注:在Linux中,最常见的压 ...

  4. 在vue中获取不到canvas对象? 两种解决办法。

    1. mounted 钩子函数 初次肯定获取到id 2. 如果canvas父级用到了v-if 请改成v-show ,vue Dom节点 重新渲染导致methods 方法获取不到对象.

  5. pytest_fixture-----conftest共享数据及不同层次共享

    场景:你与其他测试工程师合作一起开发时,公共的模块要在不同文件中,要 在大家都访问到的地方. 解决:使用conftest.py 这个文件进行数据共享,并且他可以放在不同位置起 着不同的范围共享作用. ...

  6. 微信小程序开发简易计算器改进版

    微信小程序开发计算器有多种方法,但是大部分代码比较复杂.不容易理解.本案例进行了改进,主要是组件bindtap属性绑定的自定义函数clickBtn(),采用了switch语句,使得代码结构更加清晰,学 ...

  7. PHP算法之电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[" ...

  8. js求三个数的最大值运算

    js代码: <script> // var num1 = 32, // num2 = 43, // num3 = 98; // if (num1 > num2) { // if (n ...

  9. Dart编程循环

    有时,某些指令需要重复执行.循环是一种理想的方法.循环表示必须重复的一组指令.在循环的上下文中,重复被称为迭代 . 下图说明了循环的分类 让我们开始讨论确定循环.迭代次数是确定/固定的循环称为确定循环 ...

  10. sql语句创建表

    create table `search_custom_mall` ( `id` ) NOT NULL PRIMARY KEY AUTO_INCREMENT, `uid` ) NOT NULL, `n ...