leetcode 211. Add and Search Word - Data structure design Trie树
写一个数据结构, 支持两种操作。 加入一个字符串, 查找一个字符串是否存在。查找的时候, '.'可以代表任意一个字符。
显然是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树的更多相关文章
- 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 ...
- [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 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,搜索单词,前缀树,字典树 ...
随机推荐
- Windows Azure Camp---漫步云端,创意无限
不再需要一系列繁杂的网银密码,一键搞定所有的支付:与朋友约会时通过实时分享地理位置迅速找到对方,这些都可以在WindowsAzure平台得以实现.在刚刚结束的2013年微软学生夏令营中,来自全国30所 ...
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...
- Java图形化界面设计——布局管理器之CardLayout(卡片布局)
- Jquery $.extend的重载方法详述
1 $.extend(result,item1,item2,item3,........) -这个重载方法主要是用来合并,将所有的参数都合并到result中,并返回result,但是这样会破坏res ...
- jsp 有哪些内置对象?作用分别是什么? 分别有什么方 法?
JSP共有以下9种基本内置组件 request对象 客户端请求,此请求包含来自GET/POST的请求参数,通过它才能了解到客户的需求,然后做出相应. response对象 ...
- 数组名和指针的深入理解(C++)
指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.魔幻数组名请看程序(本文程序在WIN32平台下编译): #includ ...
- Problem E: Automatic Editing
Problem E: Automatic EditingTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 3[Submit][Status ...
- La=LaULb (单链表)
#include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...
- PHP调试工具 《Kint》
Kint使用,简单介绍 是一个简单又强大的PHP调试工具. 1.kint 是什么? kint是用绝对易人识辨的方式展示PHP调试的数据. 换句话说,它可以取var_dump(),debug_blick ...
- html 表头固定
<div style="width: 100%; min-width: 300px; padding-right: 10px;"> <table style=&q ...