Trie 树的一个应用

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.

class TrieNode{
public:
TrieNode* child[];
bool isWord = false;
TrieNode() : isWord(false){
for(auto &a : child)
a = nullptr;
} }; class WordDictionary{
private:
TrieNode* root = new TrieNode();
public:
void addWord(string word){
TrieNode* p = root;
for(auto &a : word){
int i = a - 'a';
if(p->child[i] == nullptr) p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
} bool search(string word){
int n = word.length();
return search(word,n,,root);
} bool search(string& word,int n,int pos,TrieNode* cur){
if(pos == n) return cur->isWord;
if(cur == nullptr) return false; if(word[pos] == '.'){
for(int i=;i<;i++){
if(cur->child[i]){
if(search(word,n,pos+,cur->child[i])){
return true;
}
}
}
}else{
int i = word[pos] - 'a';
if(cur->child[i])
return search(word,n,pos+,cur->child[i]);
}
return false;
}
};

Add and Search Word的更多相关文章

  1. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

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

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

  3. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

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

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

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

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

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

  9. 211. Add and Search Word - Data structure design

    题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...

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

随机推荐

  1. iOS版本更新的App提交审核流程

    App的版本更新估计是在所难免的了!更新App和新的App发布有何不同了?今天我们一起来看看吧!在发布App的时候我们需要通过开发者帐号——(申请)——>发布证书(需要钥匙串对证书签名也叫加密( ...

  2. SQL基础语句(提升)

    1.复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 2.拷贝表 insert into b(a,b,c) select d ...

  3. js当中的声明和初始化的顺序

    if(!("a" in window)) { var a=1; } alert(a); 这里的alert出来undefined 这句话就相当于 var a; if(!(“a” in ...

  4. javap反编译解释外部类直接使用内部类private字段的原理

    2016-07-04 15:56:39 我们都知道: 1.内部类可以直接访问外部类的private字段和方法: 2.非静态内部类持有外部类的引用: 3.外部类可以直接访问内部类的private字段和方 ...

  5. Android studio 提示:Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.参考来源:http://my.oschina.net/fyyy/blog/519353 按照下图,svn相关选项不要选.

  6. what is service?

    SERVICE n.服务,服侍:服务业:维修服务:服役 vt.检修,维修:向…提供服务:保养:满足需要 adj.服务性的:耐用的:服现役的 更多详情:http://dict.baidu.com/s?w ...

  7. [DNS-BIND]网络初始化

    1.创建ns_g_socketmgr: 首先,套接字管理器是全局唯一的,与有多少个网络接口无关,全局变量定义在/bin/named/include/named/globals.h: EXTERN is ...

  8. Cordova+ionic 开发hybird App --- 开发环境搭建

    Cordova 开发hybird App 开发环境搭建 一.一些基础概念: Ant : 简单说来可以这么理解,如果你用记事本写JAVA程序,然后在cmd里输入javac命令编译它,但是有一天你发现每次 ...

  9. Large Margin DAGs for Multiclass Classification

    Abstract We present a new learning architecture: the Decision Directed Acyclic Graph (DDAG), which i ...

  10. Python开发入门与实战21-订阅事件(subscribe)

    21. 订阅事件(subscribe) 新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,在新用户关注公众平台后为新用户提供一些简明扼要的公众号说明 事件推送(event): 接收 ...