字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树)
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- 【刷题-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 ...
- (*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 ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [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 ...
- [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 ...
随机推荐
- 【Python】使用Python压缩文件/文件夹
[Python压缩文件夹]导入“zipfile”模块 def zip_ya(startdir,file_news): startdir = ".\\123" #要压缩的文件夹路径 ...
- 移动端跨平台应用开发(ios、Android、web)- Flutter 技术
关键词:Google 出品:Dart语言:Flutter Engine引擎:响应式设计模式:原生渲染:免费并且开源 一.简介 Flutter 是谷歌2018年发布的跨平台移动UI框架.作为谷歌的开源移 ...
- LG3768 简单的数学题
P3768 简单的数学题 题目描述 输入一个整数n和一个整数p,你需要求出$(\sum_{i=1}^n\sum_{j=1}^n ijgcd(i,j))~mod~p$,其中gcd(a,b)表示a与b的最 ...
- 什么是ARP协议?
ARP协议,全称“Address Resolution Protocol”,中文名是地址解析协议, 使用ARP协议可实现通过IP地址获得对应主机的物理地址(MAC地址). 在TCP/IP的网络环境下, ...
- Django源码分析rest_framework 关于re_path('^publish/', views.PublishView.as_view()总结
1. ApiView 定义一个cbc视图 class BookView (APIView):pass re_path(r"books/$", views.BookView.as_v ...
- dedecms列表页使用noflag
最近小编使用dedecms遇到列表页需要使用noflag,在网上找了一圈都是直接替换代码,试用了一下并不能解决问题. 以下是小编自己根据资料整理的...多说一句由于各个编辑器打开的方式可能代码不在这一 ...
- netty: marshalling传递对象,传输附件GzipUtils
netty: marshalling传递对象,传输附件GzipUtils 前端与服务端传输文件时,需要双方需要进行解压缩,也就是Java序列化.可以使用java进行对象序列化,netty去传输,但ja ...
- 5、Hadoop 2.6.5 环境搭建
下载 地址:http://archive.apache.org/dist/hadoop/common/ sudo wget http://archive.apache.org/dist/hadoop/ ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- VS - Paginated
BootstrapPagination.cshtml @model PaginationModel <div class="pagination"> <ul> ...