实现一个 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 构成的。
  • 保证所有输入均为非空字符串。

前缀树的基础原理实现

  1. class Trie {
  2. public:
  3. struct TrieNode
  4. {
  5. TrieNode() : isword(false), children(26, nullptr){}
  6. ~TrieNode()
  7. {
  8. for(TrieNode* child : children)
  9. {
  10. if(child)
  11. delete child;
  12. }
  13. }
  14. bool isword;
  15. vector<TrieNode*> children;
  16. };
  17. /** Initialize your data structure here. */
  18. Trie() : root(new TrieNode())
  19. {
  20. }
  21. TrieNode* root;
  22. const TrieNode* Find(const string &prefix) const
  23. {
  24. const TrieNode* newRoot = root;
  25. for(const char c : prefix)
  26. {
  27. newRoot = newRoot ->children[c - 'a'];
  28. if(newRoot == nullptr)
  29. break;
  30. }
  31. return newRoot;
  32. }
  33. /** Inserts a word into the trie. */
  34. void insert(string word)
  35. {
  36. TrieNode* newRoot = root;
  37. for(char c : word)
  38. {
  39. if(newRoot ->children[c - 'a'] == nullptr)
  40. {
  41. newRoot ->children[c - 'a'] = new TrieNode();
  42. }
  43. newRoot = newRoot ->children[c - 'a'];
  44. }
  45. newRoot ->isword = true;
  46. }
  47. /** Returns if the word is in the trie. */
  48. bool search(string word)
  49. {
  50. const TrieNode* tmp = Find(word);
  51. return tmp != nullptr && tmp ->isword == true;
  52. }
  53. /** Returns if there is any word in the trie that starts with the given prefix. */
  54. bool startsWith(string prefix)
  55. {
  56. return Find(prefix) != nullptr;
  57. }
  58. };

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. print的简单使用

    import time num=20 for i in range(num): print("#", end="") 结果如下: 加个强制刷新 num=20 f ...

  2. python调用tushare获取上市公司管理层人员薪酬和持股

    接口:stk_rewards 描述:获取上市公司管理层薪酬和持股 积分:用户需要2000积分才可以调取,具体请参阅本文最下方积分获取办法 注:tushare包下载和初始化教程,请查阅我之前的文章 输入 ...

  3. 创建UI的线程才能访问UI,那么怎样才算访问UI呢

    只有创建UI元素的线程(主线程又叫UI线程)才能访问UI元素.在UI线程中工作,不会有这个问题. 在后台线程中,如果直接访问UI元素,会抛出 “调用线程无法访问此对象,因为另一个线程拥有该对象” 异常 ...

  4. C#异步编程----async和await组合的写法

    微软示例: private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync ...

  5. 如何将sql查询出的列名用注释代替?

    如何将sql查询出的列名用注释代替? 大家正常的工作的时候,会有这样的要求,客户想要看下原始数据,但是呢.前台导出又麻烦,这时候只能从数据库拷贝出来一份.但是呢,数据库里面的字段客户又看不明白,只能用 ...

  6. ThreadLocal知识点

    ThreadLocal是什么 ThreadLocal 表面上看他是和多线程,线程同步有关的一个工具类,但其实他与线程同步机制无关.线程同步机制是多个线程共享同一个变量,而ThreadLocal是为每个 ...

  7. 二进制中1的个数(Java实现)

    问题: 输入一个整数,求其二进制中1的个数 看到这个问题,我们应该想到数的位运算: 解法一:我们每次将此数&1 ,如果结果等于1,证明此数的最后一位是1,,count++: 然后在将数右移一位 ...

  8. Mybatis枚举转换

    自定义mybatis枚举转换,原理是如果用户没有定义自己的枚举转换工具,mybatis在解析枚举类时会自动获取mybatis的BaseTypeHandler,来转换枚举类,我们只需要重写这个枚举转换器 ...

  9. leetcode-126-单词接龙

    题目描述: class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: list) -> list ...

  10. MapReduce分区数据倾斜

    什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜   某一个区域的数据量要远远大于其他区域 2.数据大小倾斜  ...