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. repcache实现memcached主从

    1.repcached介绍 repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步 ...

  2. XlFileFormat Enumeration

    https://msdn.microsoft.com/zh-cn/library/office/ff198017.aspx

  3. 深入浅出MFC[摘记]

    1.Windows程序的运行本质:基于消息,事件驱动(Message Based,Event Driven).2.程序调用GetMessage API循环获取消息,程序的生命靠它来推动. MSG ms ...

  4. Monkey for iOS(CrashMonkey4IOS)

    CrashMonkey4IOS介绍 支持真机测试.模拟器测试 支持收集系统日志(Systemlog).崩溃日志(Crashlog).instrument行为日志 支持测试报告截图,绘制行为轨迹 支持测 ...

  5. UWP 动画系列之模仿网易云音乐动画

    一.前言 最近在弄毕业设计(那时坑爹选了制作个UWP商店的APP),一个人弄得烦躁,在这里记录一些在做毕业设计时的学习过程.由于我的毕业设计是做一个音乐播放器,那么Windows商店上优秀的软件当然是 ...

  6. -canOpenURL: failed for URL: "" - error: "(null)" , iOS9 App传输安全 支持http 解决方案

    -canOpenURL: failed for URL: "CamCardHDOpenAPI:*" - error: "(null)" This app is ...

  7. RHEL7修改swappiness

    linux系统swappiness参数在内存与交换分区间优化 2014-08-14 10:24:19分类: Linux swappiness的值的大小对如何使用swap分区是有着很大的联系的.swap ...

  8. @autoreleasepool在MRC和ARC中的区别

    对于@autoreleasepool {} (1)在ARC中会销毁所有在里面创建的对象,即使你用外面的Strong指针指向他 (2)在MRC中如果有外部的强指针指向,不会销毁对象,retainCoun ...

  9. Qt之Qwt学习之安装

    QWT+qtcreator 编译.安装使用 目录:一.Qwt简介 二.QWT编译 一.Qwt简介 QWT:Qt Widgets for Technical Applications,是开源的2D绘图库 ...

  10. javascript运动应用

    多物体运动框架: 1.多个盒子同时运动:move(obj,target)多一个参数 <!DOCTYPE html><html><head> <title> ...