题目链接

写一个数据结构, 支持两种操作。 加入一个字符串, 查找一个字符串是否存在。查找的时候, '.'可以代表任意一个字符。

显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可以。 具体dfs方法看代码。

struct node
{
node *next[];
int idEnd;
node() {
memset(next, NULL, sizeof(next));
idEnd = ;
}
};
class WordDictionary {
public:
// Adds a word into the data structure.
node *root = new node();
void addWord(string word) {
node *p = root;
int len = word.size();
for(int i = ; i<len; i++) {
if(p->next[word[i]-'a'] == NULL)
p->next[word[i]-'a'] = new node();
p = p->next[word[i]-'a'];
}
p->idEnd = ;
return ;
}
int dfs(string word, int pos, node *p) { //pos是代表当前是在哪一位。
if(pos == word.size())
return p->idEnd;
int len = word.size();
for(int i = pos; i<len; i++) {
if(word[i] == '.') {
for(int j = ; j<; j++) {
if(p->next[j] == NULL)
continue;
if(dfs(word, pos+, p->next[j]))
return ;
}
return ;
} else if(p->next[word[i]-'a'] != NULL) {
return dfs(word, pos+, p->next[word[i]-'a']);
} else {
return ;
}
}
}
// 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 dfs(word, , root);
}
};

leetcode 211. Add and Search Word - Data structure design Trie树的更多相关文章

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

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

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

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

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

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

  6. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

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

  8. 【刷题-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 ...

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

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

随机推荐

  1. Windows Latex 中日文字体设置例

    中文字体例: \documentclass[CJK]{article} \usepackage{CJKutf8} \newcommand{\songti}{\CJKfamily{song}} % 宋体 ...

  2. Java图形化界面设计——布局管理器之CardLayout(卡片布局)

  3. kohana(3.2)和gleez(1.1.5)的安装

    *保证在kohanna的环境下安装gleez 一.配置虚拟主机(即添加端口:例如localhost:801) 以http://www.gleezcms.com为例 1: cd /etc/apache2 ...

  4. 自定义View编译失败。Binary XML file line #255: Error inflating

    02-28 15:17:16.281: DEBUG/AndroidRuntime(391): Shutting down VM 02-28 15:17:16.281: WARN/dalvikvm(39 ...

  5. 百度 LBS 开放平台,开发人员众測计划正式启动

    Hi各位亲爱滴开发人员:   你是否以前-- 期望第一时间率先接触到百度LBS开放平台的最新功能? 期望被邀请作为最最尊贵的首批试用志愿者感受志愿者的特权? 期望自己的意见被产品经理採纳.优化功能.从 ...

  6. 远程登录阿里云上的MySQL

    近期对云和server之类的感兴趣,想要将自己的数据什么的保存到远端server.研究了阿里云和百度云.今天算是有点进步吧. 我在阿里云上申请了个免费的云server(ECS),非常可惜仅仅能用5天. ...

  7. 文件下载Demo

    知识点: //获取用户要下载的资源的名称        string name=context.Request.Params["downloadName"];        //设 ...

  8. iOS工程上传AppStore时遇到的问题“ERROR ITMS-90046”解析

    在我们将代码写完整,测试没有bug之后,我们就可以将它上传到AppStore了,上传的过程只要操作正确并不会有太大的问题,但是打包的过程中会出现一些小问题,导致打的包不能上传或者上传的时候会出现错误. ...

  9. C++ typedef

    C++ typedef 作用:用来定义类型的同义词,用作类型的说明符. 用法:typedef typeName myTypeName; 使用目的:1. 为了隐藏特定类型的实现,强调使用类型的目的.2. ...

  10. Hibernate学习之hql查询语句

    *  页面上数据的字段和数据库中字段差不多,这个时候,采用迫切连接  结构比较好,如果页面上的字段很少,要按照需求加载数据,采用带构造函数的select查询 实例讲解:转自:http://www.cn ...