Design a data structure that supports the following two operations:

void addWord(word)

bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord(“bad”)

addWord(“dad”)

addWord(“mad”)

search(“pad”) -> false

search(“bad”) -> true

search(“.ad”) -> true

search(“b..”) -> true

Note:

You may assume that all words are consist of lowercase letters a-z.

Trie这样的数据结构能够非常方便的实现字符串的查找,它的时间复杂度仅仅有O(L)。依据最以下的提示单词中仅仅有a-z的小写字母这个提示也能够想到使用Trie。

前面Trie的实现使用的递归实现的,这次尝试使用非递归实现。

主要的数据结构TrieNode也做了一些改变,由于不须要进行统计计数,仅仅须要推断是否存在以某个节点结尾的字符串,所以使用一个标示量来推断是否存在以该字符结尾的字符串就可以。

搜索字符串使用的是递归,要是没有’.’这个字符可能用非递归也比較好实现,可是加上’.’这个字符后用非递归想了会儿没想出了可是感觉用递归会非常easy求解。

最后须要注意的是node节点的含义是字符的父节点。由于Trie树的根节点是一个空字符。

runtime:100ms

class WordDictionary {
public:
class TrieNode
{
public:
TrieNode * edges[26];//子节点
bool end;//标示是否有以这个节点结尾的字符串
TrieNode(){
for(int i=0;i<26;i++)
{
edges[i]=NULL;
}
end=false;
}
}; class Trie
{
public:
Trie(){
root=new TrieNode();
}
//加入单词使用循环实现,也能够使用递归,前面创建Trie树即使用的是递归
void addWord(string word)
{
if(word.empty())
return ; TrieNode * node=root;
int pos=0;
while(pos<word.size())
{
int char_code=word[pos]-'a';
if(node->edges[char_code]!=NULL)
{
node=node->edges[char_code];
pos++;
}
else
{
node->edges[char_code]=new TrieNode();
node=node->edges[char_code];
pos++;
}
}
node->end=true;
} //搜索使用递归实现,要是没有'.'使用循环也非常easy实现,可是加上限制条件后使用递归更easy一些
bool search(string &word,int pos,TrieNode * node)
{
if(word.empty()&&node->end)
return true; int char_code=word[pos]-'a';
if(pos==word.size()&&node->end)
return true; if(char_code=='.'-'a')
{
for(int i=0;i<26;i++)
if(node->edges[i]!=NULL&&search(word,pos+1,node->edges[i]))
return true;
}
else
{
if(node->edges[char_code]!=NULL)
return search(word,pos+1,node->edges[char_code]);
}
}
TrieNode * root; }; // Adds a word into the data structure.
void addWord(string word) {
trie.addWord(word);
} // 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 trie.search(word,0,trie.root);
} private:
Trie trie;
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

LeetCode211:Add and Search Word - Data structure design的更多相关文章

  1. LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

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

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

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

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

  4. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

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

  6. 【刷题-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 ...

  7. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

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

  8. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  9. (*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 ...

随机推荐

  1. XTU1201:模和除

    题目描写叙述 两个整数x和y,满足1<=x<=a,1<=y<=b 且x%y等于x/y的x和y的对数有多少? x%y是x除以y的余数, x/y是x除以y的商,即整数除. 输入 不 ...

  2. mysql安装出错cannot create windows service for mysql.error:0

    配置时最后一步出现不能启动mysql 解决成功的办法:[MySQL] Could not start the service MySQL 解决方法 安装mysql 5.1.33,在运行Server I ...

  3. Hadoop问题记录:Wrong FS: hdfs://hp5-249:9000/, expected: file:///

    一般在对文件操作的时候可能出现这个问题,可能是打开文件的时候出错,也可能是对文件夹进行遍历的时候出问题. 出现这样的问题通常是在eclipse中执行hadoop的时候出现,直接切换到shell下发送命 ...

  4. 2014.08.04,读书,读书笔记-《Matlab概率与数理统计分析》-第1章 MATLAB的数据基础

    第1章 MATLAB数据基础 虽然一直间或使用MATLAB,但从来没有系统的学习过,现在开始也不晚.先对几个重点或者平时忽略的要点做下笔记. %后的所有文字为注释,多条命令可以放在一行,但要用逗号或分 ...

  5. Beta冲刺提交—星期五

    课程链接: https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求链接: https://edu.cnblogs.com/ ...

  6. 关于iOS11上MJRefresh tabview刷新后,重新加载另一组数据, 回不到顶部或者头尾显示混乱等问题解决

    MJRefresh在iOS11上存在很多bug 比如在iphoenx上首尾仍会显示的问题 刷新数据后tableview置顶不上去等问题 虽然官方给出了适配方案  但是问题还没有的到解决 比如tabvi ...

  7. Obj文件和Bin文件

    本文导读:在用visual studio 编程时,会看到项目文件中含有bin和obj这两个文件夹,那么这两个文件夹具体包含一些什么东西的,具体作用是什么? 一.Bin文件夹 1.用来保存项目生成后程序 ...

  8. golden gate的DDL配置

    DDL复制的配置 目前只支持oracle和teradata的ddl复制 oracle能复制除了系统对象之外的所有对象 两种配置方法: 基于trigger的DDL:对于生产库有一定影响. 原理: 源库建 ...

  9. SQL where 条件顺序对性能的影响有哪些

    经常有人问到oracle中的Where子句的条件书写顺序是否对SQL性能有影响,我的直觉是没有影响,因为如果这个顺序有影响,Oracle应该早就能够做到自动优化,但一直没有关于这方面的确凿证据.在网上 ...

  10. 使用LayUI在页面实现加载层(图标)代码:

    实现代码: var index = layer.load({ shade: [0.4,'#def'], icon :' 实现效果: 可以使用 layer.close(index); 来控制其在什么时 ...