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

这题用set做的话可能会超时,应该自己定义数据结构,构造一个单词数,这样效率更高,在搜索的时候就是dfs了, 代码如下:

 class WordDictionary {
public:
struct TreeNode{
public:
TreeNode *child[];
bool isWord; TreeNode() : isWord(false){
for(auto & a : child)
a = NULL;
}
}; WordDictionary() {
root = new TreeNode();
} // Adds a word into the data structure.
void addWord(string word) {
TreeNode * p = root;
for(auto & a : word){
if(!p->child[a-'a'])
p->child[a-'a'] = new TreeNode;
p = p->child[a-'a'];
}
p->isWord = true;//标记一个单词
} bool searchWord(string & word, TreeNode * p, int index)
{
if(word.size() == index) return p->isWord;//这条很关键
if(word[index] == '.'){
for(auto & a : p->child){
if(a && searchWord(word, a, index+)) return true;
}
return false;//不要忘了
}else{
return (p->child[word[index] - 'a'])&&searchWord(word, p->child[word[index] - 'a'], index+);
}
} bool search(string word) {
return searchWord(word, root, );
} // WordDictionary()//进入函数后会首先执行构造函数
// : root(new TreeNode) {}
private:
TreeNode * root;
};

这回写的比较乱,见谅

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)的更多相关文章

  1. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  2. [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 ...

  3. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  4. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  5. 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 ...

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. (*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 ...

  8. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  9. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  10. [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 ...

随机推荐

  1. redis日常操作

    redis针对所有类型的日常操作: keys * ## 取出所有key keys my* ## 模糊匹配 exists name ## 存在name键返回1,否则返回0 del key1 ## 删除一 ...

  2. Storm完整例子

    import backtype.storm.spout.SpoutOutputCollector; import backtype.storm.task.TopologyContext; import ...

  3. Nginx 部署HTTPS

    Nginx 部署HTTPS 系统:Linux Centos 7.4 x64 软件:Nginx 1.12.2 注:需要阿里云申请本地域名与证书并添加下载到本地. 注:证书文件为 xxxx.pem 与 x ...

  4. mouseleave mouseout时候悬浮框不应该消失的时候消失了 css 解决办法

    要实现的效果和代码思路 简单来说就是 用一个div包着喇叭和悬浮框 悬浮事件写在这个div上 鼠标悬浮到div上的时候 悬浮框出现 最终要做成鼠标从小喇叭移动到下面的框上的时候 下面框是不会消失的. ...

  5. python windows打包

    接触过的工具有pyinstaller,或者py2exe.感觉pyinstaller更简单易用. 真正将依赖的dll打包称一个安装包还需要借助windows打包工具 Inno Setup 或 NSIS ...

  6. DDR4中的so-dimm 和component

    so-dimm :Small Outline Dual In-line Memory Module (小型双列直插式内存模块) component:直接焊接的ddr4芯片

  7. C++DFS方法全排列

    前几天看纪磊的<啊哈!算法>一书,里面讲算法讲的特别通俗细致,真的是初中生都能读得懂的算法书(我大二才读:P).这段代码很适合初学算法的同学. #include<iostream&g ...

  8. 彻底的卸载干净oracle 11g

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭:   2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit>   H ...

  9. DVWA安装

    DVWA安装: 启动xampp下的apache中间件和mysql 将dvwa放到xampp下的htdocs目录下 在浏览器输入http://127.0.0.1/dvwa 即可使用啦! 还有owasp的 ...

  10. Redis-与python交互

    安装包 到中文官网查找客户端代码 联网安装 sudo pip install redis 使用源码安装 unzip redis-py-master.zip cd redis-py-master sud ...