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. SQLServer 去掉 字段前后空格

    update Table1 set Column1 = ltrim(rtrim(Column1 ))

  2. OC中用NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...

  3. 面试复习(C++)之快速排序

    #include <iostream> using namespace std; void Quicksort(int *a,int low,int high) { if(low>h ...

  4. AWS-CDH5.5安装-软件下载

    1.下载安装介质 下载CM安装文件: [root@ip---- cm5.5.0]# wget -c -r -nd -np -k -L -A rpm http://archive-primary.clo ...

  5. ORA-06552: PL/SQL: Compilation unit analysis terminated ORA-06553: PLS-553: character set name is not recognized

    首先,确认字符集是否修改的不彻底.SELECT DISTINCT (NLS_CHARSET_NAME(CHARSETID)) CHARACTERSET,DECODE(TYPE#, 1, DECODE( ...

  6. 清华微积分-1_Ch1习题

    U3-1 Here are some sets: (1) R both and (2) ∅ both and (3) (1,+∞) open set (4) [−1,0]  closed set, - ...

  7. 初学mongodb笔记

    先下载下mongodb,这里官网下载,https://www.mongodb.com/download-center?jmp=nav,根据自己的系统选择下载, 然后解压一下:会有这个文件夹\mongo ...

  8. 译:Boost Property Maps

    传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...

  9. 计算机●编程语言●JAVA

    <Java编程思想(第4版)>    2016-04-27 12:38 ☆ 对JAVA知识面比较全的介绍.但也只是介绍,没有进入主题好好分析透彻.所以适合有几年工作经验的java码农(虽然 ...

  10. Python开发入门与实战17-新浪云部署

    17. 新浪云部署 上一章节我们介绍了如何在本地windows服务器部署python django的网站,本章我们简要说明一下如何把python django工程部署到云服务上. 本章章节我们描述如何 ...