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,搜索单词,前缀树,字典树 ...
随机推荐
- PHP 7 值得期待的新特性(下)
这是我们期待已久的 PHP 7 系列文章的第二篇.点此阅读 第一篇本文系 OneAPM 工程师编译整理. 也许你已经知道,重头戏 PHP 7 的发布将在今年到来!现在,让我们来了解一下,新版本有哪些新 ...
- linux下c++實現簡單的生產者消費者隊列模式
引言 生產者消費者是一個經典的模式 利用生產者,消費者和緩衝區降低了生產者和消費者之間的的耦合度 便於對生產者和消費者的修改 下面記錄的是一個經典的單一生產者多消費者的模式 設計思路 以隊列做為緩衝區 ...
- Android 显示大图片
主要的代码如下: BitmapFactory.Options options = new BitmapFactory.Options(); //图片解析配置 options.inJustDecodeB ...
- Android 调用系统的拍相程序进行录像
xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- POJ 1091 跳蚤 容斥原理
分析:其实就是看能否有一组解x1,x2, x3, x4....xn+1,使得sum{xi*ai} = 1,也就是只要有任意一个集合{ai1,ai2,ai3, ...aik|gcd(ai1, ai2, ...
- 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources
Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...
- ORACLE和SQL SERVER的数据同步常用方法
ORACLE和SQL SERVER的数据同步常用方法 1. 自己编程,或者第三方工具2. 在sqlserver中,使用linkedserver,访问oracle,然后编写job进行数据同步3. 在or ...
- Android开发UI之在子线程中更新UI
转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...
- awesome awesomeness
Awesome Awesomeness A curated list of amazingly awesome awesomeness.Also available on: Awesome-Aweso ...
- Oracle Length 和 Lengthb 函数说明 .(用来判断记录值里是否有中文内容)
一.官网的说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/functions088.htm#SQLRF00658 P ...