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

    客户不须要内部的实现,仅仅须要知道有这个功能就好了,(最少知识原则)

  2. Java之旅--Web.xml解析

    Windows的IIS,是用UI界面进行网站的配置.Linux以下的差点儿全部系统,都是使用配置文件来进行配置,Java容器(JBoss/Tomcat/Jetty/WebSphere/WebLogic ...

  3. 转:Oracle GoldenGate学习之Goldengate介绍

    转自:http://blog.sina.com.cn/s/blog_a32eff28010136d9.html 日志或归档日志获得数据的增删改变化,再将这些变化应用到目标数据库,实现源数据库与目标数据 ...

  4. 【BZOJ 2038】小Z的袜子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2038 [算法] 莫队算法 [代码] #include<bits/stdc++. ...

  5. python中在py代码中如何去调用操作系统的命令

    import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',10800)) sk.listen() conn,a ...

  6. Python笔记(七)

    # -*-coding:utf-8-*- # Python 文件I/O # 打印到屏幕 #print 1234567 # 读取屏幕输入 #input_str=raw_input("Pleas ...

  7. ROS-多机通信

    前言:一定要在同一路由的局域网下进行,就是两台电脑的ip要像这样:192.168.191.4和192.168.191.8,只有最后一位不同,这样就能ping通了,否则ping不同. 一.查看ip和主机 ...

  8. Nosql的实际应用场景

    怎么样把NoSQL引入到我们的系统架构设计中,需要根据我们系统的业务场景来分析,什么样类型的数据适合存储在NoSQL数据库中,什么样类型的数据必须使用关系数据库存储.明确引入的NoSQL数据库带给系统 ...

  9. js通过经纬度计算两点之间的距离

    最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...

  10. (转)Django学习之 第一章:Django介绍

    Django: Python编程Web框架 如果你上djangoproject.com你会发现对Django的如下解释: “Django is a high-level Python Web fram ...