题目链接

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

显然是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. 一步一步学数据结构之1--n(通用树)

    今天来看大家介绍树,树是一种非线性的数据结构,树是由n个结点组成的有限集合,如果n=0,称为空树:如果n>0,则:有一个特定的称之为根的结点,它只有直接后继,但没有直接前驱:除根以外的其他结点划 ...

  2. Java图形化界面设计——布局管理器之BorderLayout(边界布局)

  3. BC第二场

    GT and sequence  Accepts: 385  Submissions: 1467  Time Limit: 2000/1000 MS (Java/Others)  Memory Lim ...

  4. NGUI使用教程(1) 安装NGUI插件

    前言 鉴于当前游戏开发的大势,Unity3d的发展势头超乎我的预期,作为一个Flash开发人员,也是为Flash在游戏开发尤其是手游开发中的地位感到担忧....所以 近期一段时间都在自己学习unity ...

  5. 表单验证插件 jquery.validata 使用方法

    参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...

  6. BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )

    高斯消元解xor方程组...暴搜自由元+最优性剪枝 -------------------------------------------------------------------------- ...

  7. NOIP2010提高组] CODEVS 1069 关押罪犯(并查集)

    这道这么简单的题目还写了这么久.. 将每个会发生冲突的两人的怒气进行排序,然后从怒气大到小,将两个人放到不同监狱中.假如两人都已经被放置且在同一监狱,这就是答案. ------------------ ...

  8. PHP练习项目笔记之COOKIES

    主要是在登录和退出的时候,设置cookies.来保存登录和安全退出 1:在登录页面设置 //设置cookies的值 _setcookies($_rows['tg_username'], $_rows[ ...

  9. MYSQL异常和错误机制

    BEGIN ; ; ; START TRANSACTION; call put_playerbehavior(i_playerid,i_gameid,i_channelid,i_acttime,@a) ...

  10. 微信 php 获取ticket

    <?phpheader('content-type:text/html; charset=utf8');define('TOKEN', 'youtoken'); // TOKENdefine(' ...