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,搜索单词,前缀树,字典树 ...
随机推荐
- 谈谈怎么实现Oracle数据库分区表
谈谈怎么实现Oracle数据库分区表 数据库的读写分离 SQLSERVER性能监控级别步骤 Oracle索引问题诊断与优化(1)
- 关于Spark中RDD的设计的一些分析
RDD, Resilient Distributed Dataset,弹性分布式数据集, 是Spark的核心概念. 对于RDD的原理性的知识,可以参阅Resilient Distributed Dat ...
- Akka官方文档翻译:Cluster Specification
参加了CSDN的一个翻译项目,翻译Akka的文档.CSDN提供的翻译系统不好使,故先排版一下放在博客上. 5.1 集群规范 注意:本文档介绍了集群的设计理念.它分成两部分,第一部分描述了当前已经实现的 ...
- maven更新总结与tomcat发布方法总结
这些天来一直为不能直接把项目实时的发布到tomcat而费心思,项目使用了maven来组织,编译和运行,而maven插件的安装曾经有些问题,为此怀疑不能发布项目到tomcat是因为maven有问题,为些 ...
- PHP的执行原理/执行流程
http://www.cnblogs.com/hongfei/archive/2012/06/12/2547119.html 更深入的学习和了解可以查看下面: 风雨的博客http://www.laru ...
- SQLite入门与分析(二)---设计与概念(续)
SQLite入门与分析(二)---设计与概念(续) 写在前面:本节讨论事务,事务是DBMS最核心的技术之一.在计算机科学史上,有三位科学家因在数据库领域的成就而获ACM图灵奖,而其中之一Jim G ...
- Android安全问题 抢先开机启动
导读:我们以如何抢先开机启动为例,来说明接收无序广播的静态广播接收器的接收顺序 (注意,文本只是陈述结果,所以叫结果篇,之后的文章再给出源码分析) 首先先说一下android中的广播和广播接收器 广播 ...
- Android 使用SDcard进行文件的读取
平时我们需要在手机上面存储想音频,视频等等的大文件,以前学过使用File进行存储(使用File操作进行存储):由于考虑到手机本身的存储空间小,这时候我们需要把文件存储在SDcard中,今天自己也学习了 ...
- P151、面试题27:二叉搜索树与双向链表
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.(本质是中序遍历)二叉树结点的定义如下:struct BinaryTreeNod ...
- node.js 模块和包
Node.js 的模块和包机制的实现参照了 CommonJS 的标准,但并未完全遵循.不过两者的区别并不大,一般来说你大可不必担心,只有当你试图制作一个除了支持 Node.js之外还要支持其他平台的模 ...