【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 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* children[];
- bool end;
- TrieNode()
- {
- for(int i = ; i < ; i ++)
- children[i] = NULL;
- end = false;
- }
- };
- class WordDictionary {
- public:
- WordDictionary()
- {
- root = new TrieNode();
- }
- // Adds a word into the data structure.
- void addWord(string word) {
- TrieNode* cur = root;
- int i = ;
- while(i < word.size() && cur->children[word[i]-'a'] != NULL)
- {
- cur = cur->children[word[i]-'a'];
- i ++;
- }
- if(i == word.size())
- cur->end = true;
- else
- {
- while(i < word.size())
- {
- cur->children[word[i]-'a'] = new TrieNode();
- cur = cur->children[word[i]-'a'];
- i ++;
- }
- cur->end = true;
- }
- }
- // 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 search(word, root);
- }
- bool search(string word, TrieNode* cur)
- {
- if(cur == NULL)
- return false;
- else if(word == "")
- return (cur->end == true);
- else
- {
- if(word[] != '.')
- {
- if(cur->children[word[]-'a'] == NULL)
- return false;
- else
- return search(word.substr(), cur->children[word[]-'a']);
- }
- else
- {
- for(int i = ; i < ; i ++)
- {
- if(search(word.substr(), cur->children[i]))
- return true;
- }
- return false;
- }
- }
- }
- TrieNode* root;
- };
- // Your WordDictionary object will be instantiated and called as such:
- // WordDictionary wordDictionary;
- // wordDictionary.addWord("word");
- // wordDictionary.search("pattern");
【LeetCode】211. Add and Search Word - Data structure design的更多相关文章
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 【刷题-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 ...
- [leetcode trie]211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- (*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 ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- [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 ...
随机推荐
- THINKPHP 解决模块不存在时出现空页面的问题
遇到的问题: 最近使用THINKCMF开发了一个企业网站,因为之前客户的域名变更过,然后就发现当某个模块不存在的时候就出现了空页面 在 THINKPHP论坛 上有人说在项目里添加一个EmptyActi ...
- (转)mobile cpu上禁用alpha test的相关总结
转自:http://www.cnblogs.com/TracePlus/p/4037165.html 因为,每家芯片的特性不同,根据向framebuffer写法的不同,分为tile-based的mob ...
- 在linux 中wget 无法解析主机
vim /etc/resolv.cof 在里面加入节点 nameserver 8.8.8.8 / nameserver 8.8.4.4 即可 失败时: 成功时:
- OpenGL ES 3.0之Uniform详解
Uniform是变量类型的一种修饰符,是OpenGL ES 中被着色器中的常量值,使用存储各种着色器需要的数据,例如:转换矩阵.光照参数或者颜色. uniform 的空间被顶点着色器和片段着色器分享 ...
- Laravel的目录结构分析
根目录结构/app/bootstrap/public/vendorartisancomposer.jsonserver.php 1./app整个Laravel 目录中最需要我们注意的地方,包含设置(c ...
- 使用jstl报错:Can not find the tag library descriptor for “http://java.sun.com/jstl/core”
使用jstl报错:Can not find the tag library descriptor for “http://java.sun.com/jstl/core” 出现这个错误的原因是项目中没有 ...
- SpeechLib 应用
//引用组件:Interop.SpeechLib.dll //导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //1.SpVoi ...
- 如何设置Apache中的最大连接数
Apache的主要工作模式有两种:prefork和worker 一.两种模式 prefork模式(缺省模式) prefork是Unix平台上的默认(缺省)MPM,使用多个子进程,每个子进程只有一个线程 ...
- 推荐五星级C语言学习网站
www.cprogrammingexpert.com (此网站,配合了大量动画,每一行代码,配合一副图片) 下面截取了部分的gif动画,大家可以认真看看, 相信作者花了很多心血,去制作这些动画. sc ...
- 转:Creating a Nested ESXi 5 Environment
http://tsmith.co/2011/creating-a-nested-esxi-5-environment/ http://tsmith.co/2012/vsphere-5-1-lab-ne ...