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 ...
随机推荐
- MVC 登录后重定向回最初请求的 URL FormsAuthentication.RedirectFromLoginPage
在传统的Asp.net webForm 中如果使用 Form身份验证.登录后重定向到最初请求的页面只需使用 FormsAuthentication.RedirectFromLoginPage 但在MV ...
- python_re函数
1,贪婪和非贪婪模式 重复运算符默认是贪婪的,即会进行尽可能多的匹配 代码示例: >>> import re >>> emphasis_pattern = re.c ...
- set和multiset容器
set和multiset容器的能力 set 和multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现.当元素放入容器中时,会按照一定的排序法则自动排序,默认是 ...
- 如何在.net项目中使用全局程序集GAC
在解决已有.net网站问题过程中(之前的同事写的),发现出现dll不存在的情况,build报错 在bin目录下找不到该dll,后来发现是全局程序集,存储在C:\Windows\assembly目录下 ...
- Laravel中的查询构造器
public function query(){ //新增数据 //$bool = DB::table('wd_user')->insert(['username'=>'jack']); ...
- flex 在父窗口监听弹出窗口里的某个按钮被点击
flex 在父窗口监听弹出窗口里的某个按钮被点击 这样可以从子窗口拿回数据在父窗口处理数据,不必再子窗口中处理.在某些情形下省去了不少麻烦. /** * 右键菜单项单击事件 * changed by ...
- C# in查询
一.前言 在做项目中,我们会经常使用到 in 查询语句.那么如果我们用 EF 和 Linq 怎么写?接下来看代码 二.实例 我使用的是区域查询的例子,基本的 sql 语句如下: SELECT * FR ...
- Kibana: Unknown error while connecting to Elasticsearch
如果访问不通,可能是网络代理的问题,注意查看/etc/profile中是否设置了相关的代理. 注意修改代理后,应该执行 . /etc/profile 使配置文件立即生效.
- [Xcode 实际操作]一、博主领进门-(6)Xcode的iOS模拟器的基本使用方法
目录:[Swift]Xcode实际操作 本文将演示Xcode的iOS模拟器的基本使用方法. 在项目导航区,鼠标右键[Assets.xcassets]资源文件夹. 隔壁右侧区域左下角点击[+],打开资源 ...
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:5. 设置设备属性
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...