LeetCode211: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.
Trie这样的数据结构能够非常方便的实现字符串的查找,它的时间复杂度仅仅有O(L)。依据最以下的提示单词中仅仅有a-z的小写字母这个提示也能够想到使用Trie。
前面Trie的实现使用的递归实现的,这次尝试使用非递归实现。
主要的数据结构TrieNode也做了一些改变,由于不须要进行统计计数,仅仅须要推断是否存在以某个节点结尾的字符串,所以使用一个标示量来推断是否存在以该字符结尾的字符串就可以。
搜索字符串使用的是递归,要是没有’.’这个字符可能用非递归也比較好实现,可是加上’.’这个字符后用非递归想了会儿没想出了可是感觉用递归会非常easy求解。
最后须要注意的是node节点的含义是字符的父节点。由于Trie树的根节点是一个空字符。
runtime:100ms
class WordDictionary {
public:
class TrieNode
{
public:
TrieNode * edges[26];//子节点
bool end;//标示是否有以这个节点结尾的字符串
TrieNode(){
for(int i=0;i<26;i++)
{
edges[i]=NULL;
}
end=false;
}
};
class Trie
{
public:
Trie(){
root=new TrieNode();
}
//加入单词使用循环实现,也能够使用递归,前面创建Trie树即使用的是递归
void addWord(string word)
{
if(word.empty())
return ;
TrieNode * node=root;
int pos=0;
while(pos<word.size())
{
int char_code=word[pos]-'a';
if(node->edges[char_code]!=NULL)
{
node=node->edges[char_code];
pos++;
}
else
{
node->edges[char_code]=new TrieNode();
node=node->edges[char_code];
pos++;
}
}
node->end=true;
}
//搜索使用递归实现,要是没有'.'使用循环也非常easy实现,可是加上限制条件后使用递归更easy一些
bool search(string &word,int pos,TrieNode * node)
{
if(word.empty()&&node->end)
return true;
int char_code=word[pos]-'a';
if(pos==word.size()&&node->end)
return true;
if(char_code=='.'-'a')
{
for(int i=0;i<26;i++)
if(node->edges[i]!=NULL&&search(word,pos+1,node->edges[i]))
return true;
}
else
{
if(node->edges[char_code]!=NULL)
return search(word,pos+1,node->edges[char_code]);
}
}
TrieNode * root;
};
// Adds a word into the data structure.
void addWord(string word) {
trie.addWord(word);
}
// 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 trie.search(word,0,trie.root);
}
private:
Trie trie;
};
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
LeetCode211:Add and Search Word - Data structure design的更多相关文章
- LeetCode OJ: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面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【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 ...
- [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- (*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 ...
随机推荐
- 杭电 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【ruby项目,语言提交检查(一)】怎样高速学习ruby ?
怎样高速学习ruby ? 学习语言最快的思路. 变量,常量,变量类型,操作符. 逻辑语句如 if, else, switch, for, foreach, do while, break, 等等.要学 ...
- Nagios监控nginx服务具体过程
1在nginx 服务器上安装nrpe客户端: Nginx的服务须要监控起来.不然万一down了而不及时修复,会影响web应用.例如以下web应用上面启动的nginx后台进程[root@lb-net-2 ...
- C++实现位数组
当我们遇到大量整数排序时候为了节省内存空间我们能够考虑使用bit数组来实现,缺点是其仅仅适用于正整数. 思想: 在32位系统一个int类型占4个字节,按位来计算一个int类型能够记录32个数.因此,採 ...
- 【Java集合源代码剖析】LinkedList源代码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253 您好.我正在參加CSDN博文大赛.假设您喜欢我的文章,希望您能帮我投一票,谢 ...
- 2015.04.20,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 30
1.brothers and sisters, wives and husbands Frater: brothers; soror: sister; uxor: wife; maritus: hus ...
- 希尔加密算法(湖南师范大学第六届大学生计算机程序设计竞赛)hnuoj11552
解密 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 2, Accept ...
- mysqli简介
mysqli简介 PHP MySQLi 简介 PHP MySQLi = PHP MySQL Improved! MySQLi 函数允许您访问 MySQL 数据库服务器. 注释:MySQLi 扩展被设计 ...
- http server优雅启停原理及验证
问题背景 在 http应用程序重启时,如果直接 kill -9 使程序退出,然后再启动,会存在的问题: 旧的请求未处理完,如果服务端进程直接退出,会造成客户端连接中断(收到 RST) 新请求打过来,服 ...
- Linux,Docker,Jenkins No such file or directory
你们先休息下,我先哭哭! 今天在做交接项目的bug修改的时候,在创建文件的时候报错 No such file or directory 然后跟着路径去linux中查看了该路径,但确实存在,并且权限都是 ...