LeetCode Add and Search Word - Data structure design (trie树)
题意:实现添加单词和查找单词的作用,即实现字典功能。
思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.",能应对这三种就没有问题了。在每个单词的尾字母上标上tag=1,代表从树根到此节点有一个单词。暂时想不到更快的办法。
class WordDictionary {
public:
WordDictionary(){
tree=create();
}
void addWord(string word) {
node *tmp=tree;
node *p=; //负责创建结点
for(int i=; i<word.size(); i++)
{
if(!tmp->next[word[i]-'a']) //没有这个分支,创建它
{
p=create();
tmp->next[word[i]-'a']=p;
}
tmp=tmp->next[word[i]-'a']; //往下走
}
tmp->tag=;
} bool search(string word) {
if(DFS(tree,word.c_str())==true)//搜
return true;
return false;
}
private: struct node
{
bool tag;
node *next[]; };
node *tree;//先建立树根
node *create()
{
node *tmp=new(node);
tmp->tag=;
for(int i=; i<; i++)
tmp->next[i]=;
return tmp;
}
bool DFS(node *t,const char *p)
{
if(*(p+)=='\0')
{
if(*p=='.') //'.'刚好是最后一个
{
for(int i=; i<; i++)
if(t->next[i]&&t->next[i]->tag==)
return true;
return false; //无匹配
}
if(t->next[*p-'a'] && t->next[*p-'a']->tag==) return ;
return false;
} if(*p=='.')//要搜索全部
{
for(int i=; i<; i++)
if( t->next[i] && DFS(t->next[i],p+) )
return true;
return false;
} if( t->next[*p-'a'] && DFS(t->next[*p-'a'],p+))
return true;
return false;
}
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
AC代码
LeetCode Add and Search Word - Data structure design (trie树)的更多相关文章
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- LeetCode——Add and Search Word - Data structure design
Description: Design a data structure that supports the following two operations: void addWord(word) ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- 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 ...
随机推荐
- Ubuntu 16.04使用chrome闪屏
使用Chrome的时候上端经常出现闪动的情况, 但是速度特别快, 根本无法截图, 感觉特别扎心, 以为自己的电脑出现问题了或者显卡驱动出现问题了, 后来才发现问题, 只需要关闭Chrome的硬件加速就 ...
- 关于layer.open() 弹框的使用
在用 layer.open() 追加渲染HTML的时候,完全看不到效果,皆因layui框架需要在最后用 form.render() 方法来渲染表单才可以看到效果,写法如下: <script> ...
- Deques and Randomized Queues
1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...
- 1.5-1.6 oozie部署
一.部署 可参考文档:http://archive.cloudera.com/cdh5/cdh/5/oozie-4.0.0-cdh5.3.6/DG_QuickStart.html 1.解压oozie ...
- 3.16 使用Zookeeper对HDFS HA配置自动故障转移及测试
一.说明 从上一节可看出,虽然搭建好了HA架构,但是只能手动进行active与standby的切换: 接下来看一下用zookeeper进行自动故障转移: # 在启动HA之后,两个NameNode都是s ...
- lua中文教程【高级知识】
一.编译和运行和调试 1.lua和其他解释型语言一样,先转换成为中间码再执行 2.dofile和loadfile的区别:loadfile编译返回不执行,返回错误代码:dofile执行,返回错误信息 3 ...
- Black Box--[优先队列 、最大堆最小堆的应用]
Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...
- ASP.NET Core Web API + Angular 仿B站(三)后台配置 JWT 的基于 token 的验证
前言: 本系列文章主要为对所学 Angular 框架的一次微小的实践,对 b站页面作简单的模仿. 本系列文章主要参考资料: 微软文档: https://docs.microsoft.com/zh-cn ...
- Codevs 1523 地精部落
1523 地精部落 省队选拔赛 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 传说很久以前,大地上居住 ...
- C - Aladdin and the Flying Carpet
#include<bitset> #include<map> #include<vector> #include<cstdio> #include< ...