题意:实现添加单词和查找单词的作用,即实现字典功能。

思路:'.' 可以代表一个任何小写字母,可能是".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树)的更多相关文章

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

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

  2. [LeetCode] 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——Add and Search Word - Data structure design

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

  4. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  5. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

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

随机推荐

  1. Python2.7 urlparse学习

    urlparse模块主要是把url拆分为6部分,并返回元组.并且可以把拆分后的部分再组成一个url.主要有函数有urljoin.urlsplit.urlunsplit.urlparse等. urlpa ...

  2. Flutter实战视频-移动电商-09.首页_项目结构建立和获取数据

    09.首页_项目结构建立和获取数据 在config下创建service_url.dart 用来配置我们后端接口的配置文件 一个变量存 接口地址,一个接口方法地址 所有后天请求数据的方法都放在这个文件夹 ...

  3. Flutter实战视频-移动电商-24.Provide状态管理基础

    24.Provide状态管理基础 Flutter | 状态管理特别篇 —— Provide:https://juejin.im/post/5c6d4b52f265da2dc675b407?tdsour ...

  4. Identity Server 4 原理和实战(完结)_汇总贴

    视频地址:https://www.bilibili.com/video/av42364337 语雀地址:https://www.yuque.com/yuejiangliu/dotnet/solenov ...

  5. Laravel中使用模型对数据进行操作

    public function orm(){ //查询表的所有记录 //$user = Admin::all(); //dd($user); //查询某一条记录 //$user = Admin::fi ...

  6. AIRSDK 3.7 加载远程的含有代码的swf文件

    之前就说这个版本会解决可以加载远程的含有代码的swf文件的需求.但是,一直比较好奇这个是否行得通,还以为 Adobe 副总裁去了苹果,内部给了特殊待遇. 因为苹果一直就是不允许远程加载代码的,像js文 ...

  7. shader实例(二十二)TexGen-球面贴图SphereMap

    http://blog.sina.com.cn/s/blog_89d90b7c0102vfqz.html 球面贴图一般用于环境反射,如下图(左边为球面贴图,右边为正常贴图),一个镜面水晶球在这只猫的前 ...

  8. Unity3d的批渲染 batch rendering

    http://blog.csdn.net/leonwei/article/details/41942157 批渲染(Batch) batch render 是大部分引擎提高渲染效率的方法,基本原理就是 ...

  9. Codevs 1247 排排站

    1247 排排站 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description FJ的N头奶牛有一些共同 ...

  10. Apache与Tomcat的区别和联系

    经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...