设计一个支持以下两种操作的数据结构:

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 添加与搜索单词 - 数据结构设计的更多相关文章

  1. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  2. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

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

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

  4. [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 ...

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

  6. [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面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  8. 【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 ...

  9. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

随机推荐

  1. 2019 牛客多校第六场 D Move

    题目链接:https://ac.nowcoder.com/acm/contest/886/D 题解摘自官方题解 题目大意 有 K 个体积相同的箱子,有 N 个体积相同或相异的物品,现要按照如下策略装箱 ...

  2. JUC源码分析-集合篇:并发类容器介绍

    JUC源码分析-集合篇:并发类容器介绍 同步类容器是 线程安全 的,如 Vector.HashTable 等容器的同步功能都是由 Collections.synchronizedMap 等工厂方法去创 ...

  3. 绝对 "牛X" 的代码注释,喜欢就拿去用!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:Blankj https://github.com/Blankj/awesome-comment 写在前面的话:一时兴 ...

  4. leetcode.字符串.14最长公共前缀-Java

    1. 具体题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...

  5. 牛客网多校训练第四场C sequence

    (牛客场场有笛卡尔树,场场都不会用笛卡尔树...自闭,补题心得) 题目链接:https://ac.nowcoder.com/acm/contest/884/C 题意:给出两个序列a,b,求max{mi ...

  6. plsql连接问题

    PLsql好久没用了,今天想用的时候,发现自己忘记本地实例(system)的密码了,试了几个自己常用的密码都不对,提示“ORA-01017:invalid username/password;logo ...

  7. Django学习铺垫

    Web框架本质 所有的web服务本质都是一个socket服务端,用户浏览器就是一个socket客户端,这样就实现了自己的web框架 ,但是自己的写的框架肯定很low,各种工能崩溃,所以我们就要学习py ...

  8. shiro注解权限拦截失效解决

    今天在给项目添加注解拦截的时候,总是发现拦截不起作用,加入了这个注解按照角色来进行拦截.角色不是1的用户访问此方法都会被拦截. 测试了一会发现把注解bean配置到了shiro.xml中没有起作用.需要 ...

  9. HTML常见问题

    一.外边距,margin: 垂直外边距的重叠:在网页中相邻的垂直方向的外边距会发生外边距的重叠 所谓的外边距重叠指兄弟元素之间的相邻外边距会取最大值而不是取和. .box1{margin-bottom ...

  10. IDEA2017.3.1破解激活

    idea激活有多种方式,网上较多的是使用注册码或者填License server网址,目前(2017年8月19日)使用注册码的方式,亲测可用的只有lanyun提供的注册码,但是会在2017年11月份的 ...