Add and Search Word - Data structure design - LeetCode
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 DictNode
{
public:
DictNode *dict[];
DictNode ()
{
for (int i = ; i < ; i++)
dict[i] = NULL;
}
};
class WordDictionary {
public:
WordDictionary()
{
root = new DictNode();
}
// Adds a word into the data structure.
void addWord(string word) {
DictNode *cur = root;
for (int i = , len = word.size(); i < len; i++)
{
int loc = (int)(word[i] - 'a');
if (cur->dict[loc] == NULL)
cur->dict[loc] = new DictNode();
cur = cur->dict[loc];
}
if (cur->dict[] == NULL)
cur->dict[] = new DictNode();
}
bool help(DictNode *cur, string word)
{
if (cur == NULL) return false;
if (word.size() == )
{
if (cur->dict[] == NULL) return false;
return true;
}
if (word[] != '.')
{
int loc = (int)(word[] - 'a');
return help(cur->dict[loc], (word.size() > ? word.substr() : ""));
}
else
{
for (int i = ; i < ; i++) if (cur->dict[i] != NULL)
{
if (help(cur->dict[i], (word.size() > ? word.substr() : "")))
return true;
}
return false;
}
} // 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 help(root, word);
}
private:
DictNode *root;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
Add and Search Word - Data structure design - LeetCode的更多相关文章
- 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 ...
- 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 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 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 ...
随机推荐
- Logistic回归python实现小样例
假设现在有一些点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归.利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,依次进行分类.Lo ...
- jq阻止ajax进行多次提交
在函数定义全局变量..var Stch=falseif (Stch==true){alert('请不要重新提交');}else{Stch=true;$.ajax({type:"POST&qu ...
- IOS开发学习笔记009-OC基本知识
开始学习OC,时间不等人啊,要抓紧了. OC基本知识 新建一个以.m结尾的文件,这既是oc的程序文件.在oc程序中完全兼容C语言.编译好链接类似. oc包含头文件是使用#import <> ...
- git+jenkins持续集成二-jenkins定时构建语法:定时构建语法
构建位置:选择或创建工程_设置_构建触发器 1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月 ...
- vivo和OPPO手机刷机
vivo和OPPO手机是蓝绿两厂,定位年轻时尚女士,比较注重拍照和听音乐,其他无视. 系统很少更新,Root和刷机也比较困难,建议是直接卡刷. 小米手机耍猴(猴子精,代指懂点手机的人) 蓝绿两厂是耍猪 ...
- springdata jpa
(转自:http://www.cnblogs.com/ityouknow/p/5891443.html) 首先了解JPA是什么? JPA(Java Persistence API)是Sun官方提出的J ...
- Callable、Future、FutureTask_笔记
参考:http://blog.csdn.net/javazejian/article/details/50896505 1.Callable<V>接口 Runnable接口 public ...
- PE第n弹
PE201 -- F[i][j][k] 和为i前j个选了k个方案数.因为值域很小. PE202 -- 那个反射其实挺酷炫的,你考虑把它的镜面反射去掉,把它扩展成一个无限大的正三角网格.. PE410 ...
- 【bzoj2400】Spoj 839 Optimal Marks 按位最大流
Spoj 839 Optimal Marks Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 908 Solved: 347[Submit][Stat ...
- openssl Rsa 分段加密解密
密钥长度 1024 openssl genrsa -out rsa_private_key.pem openssl rsa -in rsa_private_key.pem -pubout -out r ...