字典树(查找树)

26个分支
作用:检测字符串是否在这个字典里面
插入、查找

字典树与哈希表的对比:
时间复杂度:以字符来看:O(N)、O(N) 以字符串来看:O(1)、O(1)
空间复杂度:字典树远远小于哈希表

前缀相关的题目字典树优于哈希表
字典树可以查询abc是否有ab的前缀

字典树常考点:
1.字典树实现
2.利用字典树前缀特性解题
3.矩阵类字符串一个一个字符深度遍历的问题(DFS+trie)
dfs树和trie树同时遍历

word searchII
dfs+hash:时间复杂度大,后面遍历到有些字符就不用遍历了。
剪枝

在写结构体struct的时候,注意必须在{}之后加分号,不然会编译报错。

//错误
struct TrieNode {
TrieNode* child[];
bool isWord = false;
TrieNode(){
for(int i = ;i < ;i++)
child[i] = NULL;
}
}
//正确
struct TrieNode {
TrieNode* child[];
bool isWord = false;
TrieNode(){
for(int i = ;i < ;i++)
child[i] = NULL;
}
};

leetcode 208. Implement Trie (Prefix Tree)

https://www.cnblogs.com/grandyang/p/4491665.html

注意:在insert或者add新的词的时候,必须在最后将isWord置为true,以表示这是一个单词的结尾。

class Trie {
public:
struct TrieNode {
public:
TrieNode *child[];
bool isWord;
TrieNode() : isWord(false) {
for (auto &a : child) a = NULL;
}
};
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
return;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
return false;
p = p->child[i];
}
return p->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* p = root;
for(char w : prefix){
int i = w - 'a';
if(!p->child[i])
return false;
p = p->child[i];
}
return true;
}
TrieNode* root;
};

211. Add and Search Word - Data structure design

https://www.cnblogs.com/grandyang/p/4507286.html

class WordDictionary {
public:
struct TrieNode {
public:
TrieNode *child[];
bool isWord;
TrieNode() : isWord(false) {
for (auto &a : child) a = NULL;
}
};
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
} /** Adds a word into the data structure. */
void addWord(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
return;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return search_core(word,root,);
}
bool search_core(string word,TrieNode* p,int index){
if(index == word.size())
return p->isWord;
if(word[index] == '.'){
for(TrieNode* tmp : p->child){
if(tmp && search_core(word,tmp,index+))
return true;
}
return false;
}
else{
int i = word[index] - 'a';
if(!p->child[i])
return false;
return search_core(word,p->child[i],index+);
}
}
TrieNode* root;
};

字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design的更多相关文章

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

  2. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  4. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  5. 【刷题-LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  6. (*medium)LeetCode 211.Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  8. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. 2013.4.24 - KDD第六天

    今天早上,中秋给我发了一个压缩包,里面有战德臣的课件,昨天我说我SQL没学好,他说给我发战徳臣课件,然后说我SQL不会的话可以看这个,还有两篇文 章<LDA数学八卦>以及<A Not ...

  2. java BIO NIO IO

    参考 https://www.cnblogs.com/zedosu/p/6666984.html 摘要: 关于BIO和NIO的理解 最近大概看了ZooKeeper和Mina的源码发现都是用Java N ...

  3. STM32移植USB驱动总结

    https://blog.csdn.net/stm32_newlearner/article/details/88095944 stm32   移植usb驱动开发 单片机 STM32单片机和51单片机 ...

  4. 防止js全局变量污染方法总结-待续

    javaScript 可以随意定义保存所有应用资源的全局变量.但全局变量可以削弱程序灵活性,增大了模块之间的耦合性.在多人协作时,如果定义过多的全局变量 有可能造成全局变量冲突,也就是全局变量污染问题 ...

  5. Validation参数验证

    在SpringBoot项目中其实已经默认引入了,如果不是sprongBoot项目则需要导入Maven <dependency> <groupId>org.hibernate.v ...

  6. CSP 初赛 知识点整理

    BIOS: BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".其实,它是一组固化到计算机 ...

  7. jsp文件上传下载组件

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  8. Django+element ui前后端不分离的博客程序

    最近把去年写的一个烂尾博客(使用了django1.11和element ui)又重新完善了一下,修改了样式和增加了新功能 github链接:https://github.com/ngauerh/Nag ...

  9. tensorflow serving 模型部署

    拉去tensorflow srving 镜像 docker pull tensorflow/serving:1.12.0 代码里新增tensorflow 配置代码 # 要指出输入,输出张量 #指定保存 ...

  10. AtCoder Grand Contest 021题解

    传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...