Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构:
void addWord(word) bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。
示例:
addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad") -> true search(".ad") -> true search("b..") -> true
说明:
你可以假设所有单词都是由小写字母 a-z 组成的。
典型的字典树,前缀树的用法
class WordDictionary {
public:
struct TrieNode
{
TrieNode(): isword(false), children(26, nullptr){}
~TrieNode()
{
for(TrieNode* child : children)
{
if(child)
delete child;
}
}
bool isword;
vector<TrieNode*> children;
};
TrieNode* root;
/** 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 c : word)
{
if(p ->children[c - 'a'] == nullptr)
{
p ->children[c - 'a'] = new TrieNode();
}
p = p ->children[c - 'a'];
}
p ->isword = true;
}
/** 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) const{
TrieNode *p = root;
for(int i = 0; i < word.size(); i++)
{
if(word[i] == '.')
{
for(int j = 0; j < 26; j++)
{
if(p ->children[j])
{
if(Find(string(word.begin() + i + 1, word.end()), p ->children[j]))
return true;
}
}
return false;
}
else
{
p = p ->children[word[i] - 'a'];
if(p == nullptr)
return false;
}
}
if(p ->isword)
return true;
return false;
}
const bool Find(string word, TrieNode* p) const
{
for(int i = 0; i < word.size(); i++)
{
if(word[i] == '.')
{
for(int j = 0; j < 26; j++)
{
if(p ->children[j])
{
if(Find(string(word.begin() + i + 1, word.end()), p ->children[j]))
return true;
}
}
return false;
}
else
{
p = p ->children[word[i] - 'a'];
if(p == nullptr)
return false;
}
}
if(p ->isword)
return true;
return false;
}
};
Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计的更多相关文章
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- [LeetCode] 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 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 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 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 【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 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
随机推荐
- Java全栈,MySQL搞透,架构手到擒来,还有面试官搞不定?
五月最后一天啦,时间过得真快,做技术的难免做了几年就感觉很迷茫,那就需要多读点书,多学点技术才能有安全感. 栈长之前推荐过不少极客时间的课程,几乎每周都推荐一个,很多朋友评论说,课程太多学不过来,今天 ...
- HTML5 Canvas知识点学习笔记
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangyibin628/article/details/30108165 canvas ① 主要作 ...
- (数据科学学习手札57)用ggplotly()美化ggplot2图像
一.简介 经常利用Python进行数据可视化的朋友一定用过或听说过plotly这样的神器,我在(数据科学学习手札43)Plotly基础内容介绍中也曾做过非常详细的介绍,其渲染出的图像以浏览器为载体,非 ...
- VisualStuido中将C#脚本封装打包DLL并调用
DLL (Dynamic Link Library)---动态链接库 首先了解下使用DLL的优势,程序运行时不用加载所有代码,只有运行到引用时,才从DLL库中取出.并且使用DLL文件还可以减小程序体积 ...
- ORACLE check view
select owner as schema_name, view_name from sys.all_views where VIEW_NAME like 'P%' order by ...
- 服务器搭建SVN
linux服务器搭建SVN https://blog.csdn.net/itbird58/article/details/80445521
- ReadWriteLock 如何使用?
QReadWriteLock从名字看就知道是读写锁的意思.和QMutex一样,QReadWriteLock也是线程同步的一种工具.那么它有什么用呢?和QMutex又有什么区别呢?写个例子瞧一瞧. 特点 ...
- Eureka的表兄弟Zookeeper理论基础
Eureka的表兄弟Zookeeper 简单介绍 Zookeeper是一个开源的分布式应用程序协调服务器,主要功能包括配置维护,域名服务,分布式同步,集群管理等 主要功能简介 一.配置维护 分布式系统 ...
- 【NIO】NIO之浅谈内存映射文件原理与DirectMemory
Java类库中的NIO包相对于IO 包来说有一个新功能是内存映射文件,日常编程中并不是经常用到,但是在处理大文件时是比较理想的提高效率的手段.本文我主要想结合操作系统中(OS)相关方面的知识介绍一下原 ...
- PHP ftp_rmdir() 函数
定义和用法 The ftp_rmdir() 函数删除 FTP 服务器上的一个目录. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE. 语法 ftp_rmdir(ftp_connectio ...