leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/
本题是在Trie树进行dfs+backtracking操作。
Trie树模板代码见:http://www.cnblogs.com/fu11211129/p/4952255.html
题目介绍:
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
.
struct Trie{
Trie *next[]; //include character '.'
bool isWord;
Trie() {
for(auto &n : this->next) n = NULL;
this->isWord = false;
}
};
class WordDictionary {
public:
Trie *root;
WordDictionary() {
this->root = new Trie();
} void insert(string s) {
Trie *p = this->root;
for(auto &c: s) {
int idx = c - 'a';
if(!p->next[idx]) p->next[idx] = new Trie();
p = p->next[idx];
}
p->isWord = true;
} void addWord(string word) {
insert(word);
} bool dfs(Trie *p, string word, int idx) {
if(idx == word.size()-) {
if(word[idx] == '.') {
for(int i=;i<;++i) {
if(p->next[i] != NULL && p->next[i]->isWord) return true;
}
return false;
}
else {
int nidx = word[idx] - 'a';
if(p->next[nidx] == NULL) return false;
else return p->next[nidx]->isWord;
}
} if(word[idx] == '.') {
for(int i=;i<;++i) {
if(p->next[i] != NULL && dfs(p->next[i], word, idx+)) return true;
}
}
else {
int nidx = word[idx] - 'a';
if(! p->next[nidx]) return false;
if(p->next[nidx] && dfs(p->next[nidx], word, idx+)) 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) {
bool flag = false;
for(int i=;i<;++i) {
if(root->next[i] != NULL) {
flag = true; break;
}
}
if(!flag) return false; return dfs(root, word, );
}
}; // 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的更多相关文章
- 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 添加和查找单词-数据结构设计
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 Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [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 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 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
随机推荐
- uva 10827
与108类似 多加了两层循环 水过 #include <iostream> #include <cstring> #include <cstdio> #includ ...
- Flume学习——Flume中事务的定义
首先要搞清楚的问题是:Flume中的事务用来干嘛? Flume中的事务用来保证消息的可靠传递. 当使用继承自BasicChannelSemantics的Channel时,Flume强制在操作Chann ...
- hibernate annotation注解 主键ID自增长
@Id @SequenceGenerator(name="increment") @GeneratedValue(strategy=GenerationType.AUTO, gen ...
- [博弈]ZOJ3591 Nim
题意: 给了一串数,个数不超过$10^5$,这串数是通过题目给的一段代码来生成的 int g = S; ; i<N; i++) { a[i] = g; ) { a[i] = g = W; } = ...
- [译]15个关于Chrome的开发必备小技巧
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- java synchronized wait
在多个线程要互斥访问数据,但线程间需要同步时——例如任务分多个阶段,特定线程负责特定阶段的情况,经常合作使用synchronized 和 wait() /** * * 计算输出其他线程锁计算的数据 * ...
- Xcode中的iOS工程模板
1. Application类型 我们大部分的开发工作都是从使用Application类型模板创建iOS程序开始的.该类型共包含7个模板,具体如下所示. Master-Detail Applicati ...
- jquery获取元素索引值index()方法
jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的 ...
- Android开发之技术文章索引
Activity: 1.PreferenceActivity Fragment: 1.fragment中套用PagerSlidingTabStrip,切换底部时viewpager消失的解决 Widge ...
- Java数组你知多少?
下面我带大家一起加深一下对Java数组的认识: 1.理解数组 数组也是一种数据类型,本身就是一种引用类型,我们从它的初始化方法,通过关键字new去完成定义及初始化就可以知道. 数组的长度是不变的,一旦 ...