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 ...
随机推荐
- angular用$sce服务来过滤HTML标签
angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...
- easyui datagrid复选框控制单选
使用easyui datagrid的时候,由于对数据表格操作太多,并且有单选和多选功能因此采用复选框.但是在单选的状态,使用CheckOnSelect和singleselect时发现,页面有明显延迟, ...
- loj2049 「HNOI2016」网络
好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...
- CSU-2221 假装是区间众数(ST表模版题)
题目链接 题目 Description 给定一个非递减数列Ai,你只需要支持一个操作:求一段区间内出现最多的数字的出现次数. Input 第一行两个整数N,Q 接下来一行有N个整数,表示这个序列. 接 ...
- Struts2拦截器原理
拦截器是struts2处理的核心,本文主要说struts2的拦截器的基本原理/实现,其它框架处理的东西就不说了,得自己再看了.struts2版本:2.2.3当一个请求来了后,从org.apache.s ...
- CSS查缺补漏篇
前面的话:关于CSS,之前我已经做过一些基础的知识点介绍.CSS主要是用来给页面设置样式的,一般说来,在一个网站中,CSS应该独立封装在一个单独的.css外部文件中.样式的设置总体来说是不难的,但是需 ...
- [HDU3480] Division [四边形不等式dp]
题面: 传送门 思路: 因为集合可以无序选择,所以我们先把输入数据排个序 然后发先可以动归一波 设$dp\left[i\right]\left[j\right]$表示前j个数中分了i个集合,$w\le ...
- BZOJ 4561 [JLoi2016]圆的异或并 ——扫描线
扫描线的应用. 扫描线就是用数据结构维护一个相对的顺序不变,带修改的东西. 通常只用于一次询问的情况. 抽象的看做一条垂直于x轴直线从左向右扫过去. 这道题目要求求出所有圆的异或并. 所以我们可以求出 ...
- BZOJ2121 字符串游戏 【dp】
题目链接 BZOJ2121 题解 dp怎么那么神呐QAQ 我们要求出最小字符串长度 我们设一个\(dp[i]\)表示前\(i\)个字符最后所形成的最短字符串长度 对于第\(i\)个字符,要么保留,就是 ...
- javascript作用域链理解
执行上下文(Execution context,简称EC) 概念 每当控制器到达ECMAScript可执行代码的时候,就进入了一个执行上下文. javascript中,EC分为三种: ...