LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))

Implement a trie with insertsearch, and startsWith methods.

实现字典树,前面好像有道题做过类似的东西,代码如下:

 1 class TrieNode {
 2 public:
 3     // Initialize your data structure here.
 4     TrieNode():isLeaf(false)
 5     {
 6         for(auto & t : dic){
 7             t = NULL;
 8         }
 9     }
10     TrieNode * dic[26];
11     bool isLeaf;
12 };
13
14 class Trie {
15 public:
16     Trie() {
17         root = new TrieNode();
18     }
19
20     // Inserts a word into the trie.
21     void insert(string word) {
22         TrieNode * p = root;
23         for(int i = 0; i < word.size(); ++i){
24             int index = word[i] - ‘a‘;
25             if(p->dic[index] == NULL)
26                 p->dic[index] = new TrieNode();
27             p = p->dic[index];
28         }
29         p->isLeaf = true;
30     }
31
32     // Returns if the word is in the trie.
33     bool search(string word) {
34         TrieNode * p = root;
35         for(int i = 0; i < word.size(); ++i){
36             int index = word[i] - ‘a‘;
37             if(p->dic[index] == NULL)
38                 return false;
39             p = p->dic[index];
40         }
41         return p->isLeaf;
42     }
43
44     // Returns if there is any word in the trie
45     // that starts with the given prefix.
46     bool startsWith(string prefix) {
47         TrieNode * p = root;
48         for(int i = 0; i < prefix.size(); ++i){
49             int index = prefix[i] - ‘a‘;
50             if(p->dic[index] == NULL)
51                 return false;
52             p = p->dic[index];
53         }
54         return true;
55     }
56
57 private:
58     TrieNode* root;
59 };
60
61 // Your Trie object will be instantiated and called as such:
62 // Trie trie;
63 // trie.insert("somestring");
64 // trie.search("key");
时间: 2024-10-13 14:15:25

LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))的相关文章

[LeetCode][JavaScript]Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. https://leetcode.com/problems/implement-trie-prefix-tree/ 实现字典树,每个节点至多有26个子孙,代表26个字母. 每个节点都有三个属性,key, isWord以及字典(哈希表,提高访问速度,也可以用数组),因为JS可以扩展实例化的对象,直接用下标访问对象,不需

LeetCode 208. Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 这道题让我们实现一个重要但又有些复杂的数据结构-字典树, 又称前缀树或单词查找树,详细介绍可以参见网友董的博客,例如,一个保存了8个键的trie结构,"A", "to", "tea&quo

[LeetCode] 208. Implement Trie (Prefix Tree) Java

题目: Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 题意及分析:实现一个字典树或者叫前缀树的插入.删除和startsWith判断.这里定义一个trieNode类作为字典树的节点类.然后有一个chilrden数组保存子节点,一个isWord变量来判断从根节点到该节点是否是一

Java for LeetCode 208 Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 解题思路: 参考百度百科:Trie树 已经给出了详细的代码: JAVA实现如下: class TrieNode { // Initialize your data structure here. int num;// 有多少单

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录 目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码 概述 Trie树,又称为字典树.单词查找树或者前缀树,是一种用于快速检索的多叉数结构.例如,英文字母的字典树是26叉数,数字的字典树是10叉树. Trie树的基本性质有三点,归纳为: 根节点不包含字符,根节点外每一个节点都只包含一个字符. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 每个节点的所有子节点包含的字符串不相同. Trie树基本实现 我们通过Lee

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri

Implement Trie (Prefix Tree) ——LeetCode

Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 实现一个字典树. 好久不做题,没感觉啊,TreeNode用一个布尔变量表示是否是一个合法单词的结尾即可,一开始还用cnt来计数,search的时候比较麻烦. class TrieNode { // Initialize your

[leetcode trie]208. Implement Trie (Prefix Tree)

实现一个字典树 1 class Trie(object): 2 3 def __init__(self): 4 self.root = TrieNode() 5 6 def insert(self, word): 7 cur = self.root 8 for i in word: 9 cur = cur.children[i] 10 cur.is_word = True 11 12 13 def search(self, word): 14 cur = self.root 15 for i i

[leedcode 208] Implement Trie (Prefix Tree)

Trie树又被称为字典树.前缀树,是一种用于快速检索的多叉树.Tried树可以利用字符串的公共前缀来节省存储空间. 但如果系统存在大量没有公共前缀的字符串,相应的Trie树将非常消耗内存.(下图为Wiki上的Trie树示意图, https://en.wikipedia.org/wiki/Trie) 子节点用hashMap表示 isWord标记从根节点到该节点是否表示一个单词,还是另一单词的前缀. Implement a trie with insert, search, and startsWi

[LeetCode-JAVA] Implement Trie (Prefix Tree)

题目: Implement a trie with insert, search, and startsWith methods. // Your Trie object will be instantiated and called as such:// Trie trie = new Trie();// trie.insert("somestring");// trie.search("key"); 思路: 题目的内容是实现一个前缀树,用来存储string的,因