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,搜索单词,前缀树,字典树 ...
随机推荐
- jsp 获取表单值, 提交类型为multipart/form-data处理
//tt.jsp<script type="text/javascript"> function doSubmit(){ alert("aaaaaa" ...
- net.sf.json.JSONException: Object is null
出现这个错误的原因是net.sf.json.JSONArray或JSONObject转换时,对象内包含另一个对象,而该被包含的对象为NULL,所以抛出异常. 补充: 最可恨的是,明明转换的时候已经成功 ...
- [itint5]单词变换
http://www.itint5.com/oj/#42 基本上就是word ladder.直接来BFS,记录前驱. vector<string> transform(set<str ...
- uva 993 Product of digits (贪心 + 分解因子)
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
- 使用dreamever去掉文件头部BOM(bom)信息 From 百度经验
本文来此百度经验: 地址为:http://jingyan.baidu.com/article/3f16e003c3dc172591c103e6.html OM主要处理浏览器窗口与框架,但事实上,浏览器 ...
- SharePoint2013切换帐户登录菜单
SharePoint2013帐户姓名显示的地方没有切换帐户的菜单,这个功能对于终端用户是可有可无的,但对于sharepoint管理员或sharepoint开发人员来讲,却是一个很重要的菜单,由于经常要 ...
- git stash
https://git-scm.com/docs/git-stash NAME git-stash - Stash the changes in a dirty working directo ...
- git删除中文文件
git中出现如下代码时,是因为文件中包含中文.而且我们也无法用 git rm name 命令来删除该文件. deleted: "chrome_plugin/source_file/iHub\ ...
- 中国VR公司的详尽名单
中国VR公司的详尽名单 <VR圈深度投资报告一:2014年以来所有VR/AR融资事件> 特征一.投资机构观望居多 尽管VR在媒体和二级市场炒得很热,但大多风险投资机构却慎于出手,以观望 ...
- poj3216
这是一道描述非常不清楚的题目 首先解释一下,题目中的ti是任务开始时间不是结束时间, 然后维修人员可以理解为可以再任意时间从公司出发: 好,首先不难想到用floyd预处理一下: 然后我们把每个任务看成 ...