字典树(查找树)

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. php 常用字符串函数总结

    php里面自带的字符串函数,日期函数,数组函数等,有时候可以帮助我们解决很复杂的问题,运用起来也比较简单. 下面总结了一下常用的字符串函数. addcslashes — 为字符串里面的部分字符添加反斜 ...

  2. python 杂记20191102

    上下文管理器: def __exit__(self, exc_type, exc_val, exc_tb):若exc_tb不是空,则说明有异常返回值只能是true或false,若是false,则会把之 ...

  3. navicat连接oracle报错:ORA-12737 Instant Client Light:unsupported server character set ZHS16GBK

    今天使用Navicat连接Oracle数据库,报了下面的这个错误:“ORA-12737 Instant Client Light:unsupported server character set ZH ...

  4. C语言蓝桥杯比赛原题和解析

    蓝桥杯:在计算机编程领域,是具有一定含金量的竞赛,用于选拔信息技术人才. 一般分为多个领域,其中包含了C/C#/C++/Java/Python等编程语言的测试题,多为算法的设计题. 下面,在搜题过程中 ...

  5. [GCP] Goolge compute Engine

    Which of the following is a PAAS option for hosting web apps on GCP? App Engine standard or flexible ...

  6. hello world 程序的生成过程

    一个c / c ++文件需要经过预先(预处理),编译(编译),编译(汇编)和链接(链接)等四步,才能生成可执行程序. 在日常编译中,通常“编译”统称这四步: gcc -c xxx .s:汇编 gcc ...

  7. Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明(转载)

    一.自适应游标共享(Adaptive Cursor Sharing) 说明 1.1 ACS概述绑定变量使Oracle DB 可以为多条SQL 语句共享单个游标,以减少分析SQL 语句所使用的共享内存量 ...

  8. Greenplum Segment 的检测机制

    Greenplum集群具有较好的容错性和高可用性,其中一点就体现在segment镜像机制上.接下来本文会简单地阐述segment的作用以及segment镜像机制是如何保证GP高可用的. Segment ...

  9. 使用echarts生成海友网企业全国分布地图

    不分类别的效果 不同分类的分布效果图 从海友网获取各个企业名单保存进mysql cmfishhelper.py 从下列网址得到各个企业名片的网址保存进表cmfish cds = get_cds() h ...

  10. 使用状态文件+vigil 监控系统状态

    vigil 是一个不错的系统可用性报告系统,具有还不错的ui 界面,同时也有通知配置,以下是一个简单的 demo 使用状态文件,以及http body 匹配的模式进行web 应用状态的监控,只是简单的 ...