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

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. 剑指offer——03二维数组中的查找

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  2. PHP对象在内存中的分配(转载)

    http://www.cnblogs.com/hongfei/archive/2012/06/12/2547120.html 对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据 ...

  3. 【Codeforces】450 B(div2)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意: 求这个的第n项. 题解:$f_{i+1} = f_i - f_{i-1} $ \begi ...

  4. SB般的“WE战队”输掉了比赛

    事实再一次证明,对于LOL这种游戏,国服选手是根本就不能有一点期待的, 国服环境太好了,赢了可以吹,输了不能骂,自信心极度膨胀,估计WE俱乐部都没有个心理咨询师, 下岗了还可以再卖卖脸,卖卖饼, 国服 ...

  5. Eclipse规范注释及注释文档的生成

    Eclipse作为JavaIDE(Integrated Development Environment,集成开发环境),可以通过设置自动添加Javadoc注释信息,如@author 作者名.@vers ...

  6. Keepalived+LVS+nginx搭建nginx高可用集群

    1. 环境准备 1. VMware; 2. 4台CentOs7虚拟主机:192.168.122.248,192.168.122.68, 192.168.122.110, 192.168.122.167 ...

  7. MySQL主从复制&读写分离&分库分表

    MySQL主从复制 MySQL的主从复制只能保证主机对外提供服务,从机是不提供服务的,只是在后台为主机进行备份数据 首先我们说说主从复制的原理,这个是必须要理解的玩意儿: 理解: MySQL之间的数据 ...

  8. PHP ftp_rawlist() 函数

    定义和用法 ftp_rawlist() 函数返回 FTP 服务器上指定目录中文件的详细列表. 语法 ftp_rawlist(ftp_connection,dir,recursive) 参数 描述 ft ...

  9. Delphi Xml

    用递归方法,使用 xml 文档生成 Treeview 树形视图.由于是动态生成,所以可以通过修改 xml 的逻辑来定制 Treeview 的结构,从而实现了 xml 对 Treeview 的动态配置, ...

  10. char*转LPCWSTR【转载】

    文章转载自https://blog.csdn.net/zhouxuguang236/article/details/8761497 通过MultiByteToWideChar函数转换 MultiByt ...