【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 operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
For example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
class TrieNode
{
public:
TrieNode* children[];
bool end;
TrieNode()
{
for(int i = ; i < ; i ++)
children[i] = NULL;
end = false;
}
}; class WordDictionary {
public:
WordDictionary()
{
root = new TrieNode();
} // Adds a word into the data structure.
void addWord(string word) {
TrieNode* cur = root;
int i = ;
while(i < word.size() && cur->children[word[i]-'a'] != NULL)
{
cur = cur->children[word[i]-'a'];
i ++;
}
if(i == word.size())
cur->end = true;
else
{
while(i < word.size())
{
cur->children[word[i]-'a'] = new TrieNode();
cur = cur->children[word[i]-'a'];
i ++;
}
cur->end = 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) {
return search(word, root);
} bool search(string word, TrieNode* cur)
{
if(cur == NULL)
return false;
else if(word == "")
return (cur->end == true);
else
{
if(word[] != '.')
{
if(cur->children[word[]-'a'] == NULL)
return false;
else
return search(word.substr(), cur->children[word[]-'a']);
}
else
{
for(int i = ; i < ; i ++)
{
if(search(word.substr(), cur->children[i]))
return true;
}
return false;
}
}
} TrieNode* root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

【LeetCode】211. Add and Search Word - Data structure design的更多相关文章
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 【刷题-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 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 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 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 ...
- (*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
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- [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 ...
随机推荐
- 3D视频的播放
3D视频的播放 人眼产生立体效果的条件有两个: 1.须要左右眼两路影像,这两路影像是不同的.具有正确的视差: 2.进入左右眼的影像要全然分离.左影像进左眼,右影像进右眼. 第一条是对3D视频源提出的要 ...
- (转) Unity3D 使用Texturepacker打包工具制作NGUI(Atlas)图集
转自:http://www.unitymanual.com/thread-37485-1-1.html 由于NGUI AtlasMaker对打包的优化不好,容易打出很大的图集,很多部分都是浪费的,所有 ...
- js的技巧
字典: if(tac["detail"][sid] || tac["detail"][sid]==0) //判断某项是否存在,0为真 tac["det ...
- mongodb自动关闭:页面文件太小,无法完成操作
在一台两G内存的win server 2008电脑上运行一个程序,一段时间后mongod自动停止,发现日志文件最后有这样的错误: 2014-11-30T00:32:32.914+0800 [conn3 ...
- Springmvc 上传文件MultipartFile 转File
转自:http://blog.csdn.net/boneix/article/details/51303207 业务场景:ssm框架 上传文件到应用服务器过程中要传到专有的文件服务器并返回url进行其 ...
- 全国第二届Revit开发实战训练营在北京圆满落幕
由北京橄榄山软件公司与筑城网校中国BIM培训网共同举办的"全国第二届Revit开发实战训练营于1月1日在筑城网校培训教室如期开班. 參加此次培训的有上海同济大学建筑设计研究院(集团)有限公司 ...
- 看见上帝的 10 个公式……
原文 Top Ten Greatest Equations Ever 本文内容 No.1 麦克斯韦方程组 No.2 欧拉方程 No.3 牛顿第二定律 No.4 毕达哥拉斯定理 No.5 薛定谔方程 N ...
- 学习 Linux,302(混合环境): 概念
http://www.ibm.com/developerworks/cn/linux/l-lpic3-310-1/ 概述 在本文中,学习以下概念: Server Message Block (SMB) ...
- did not call through to super.onCreate()
android.util.SuperNotCalledException: Activity {com.xkyiliao.xkhospital/com.xkyiliao.xkhospital.acti ...
- 算法笔记_231:网格中移动字母(Java)
目录 1 问题描述 2 解决方案 1 问题描述 2x3=6个方格中放入ABCDE五个字母,右下角的那个格空着.如图[1.jpg]所示. 和空格子相邻的格子中的字母可以移动到空格中,比如,图中的C和 ...