LeetCode *** 211. Add and Search Word - Data structure design(字典树)
来源:互联网 发布:台企怎么样知乎 编辑:程序博客网 时间:2024/06/07 15:45
题目:
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 lettersa-z
or .
. A .
means it can represent any one letter.
For example:
addWord("bad")addWord("dad")addWord("mad")search("pad") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
分析:
代码:
struct TrieNode { public: TrieNode *next[26]; bool isStr = false; TrieNode() { memset(next, 0, sizeof(next)); } }; class WordDictionary { public: TrieNode *root = new TrieNode(); // Adds a word into the data structure. void addWord(string word) { int len = word.length(); TrieNode *tmp = root; for (int i = 0; i<len; ++i) { if (tmp->next[word[i] - 'a'] == NULL)tmp->next[word[i] - 'a'] = new TrieNode(); tmp = tmp->next[word[i] - 'a']; } tmp->isStr = 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) { if (word.size() == 0)return true; TrieNode *tmp = root; if (word[0] == '.') { bool res = false; for (int i = 0; i<26; ++i) if (tmp->next[i] != NULL)res |= (search(word.substr(1), tmp->next[i])); return res; } if (tmp->next[word[0] - 'a'] == NULL)return false; return search(word.substr(1), tmp->next[word[0] - 'a']); } bool search(string word, TrieNode *tmp) { if (word.size() == 0 && tmp->isStr)return true; if (word.size() == 0 || (word.size() && tmp == NULL))return false; if (word[0] == '.') { bool res = false; for (int i = 0; i<26; ++i) if (tmp->next[i] != NULL)res |= (search(word.substr(1), tmp->next[i])); return res; } if (tmp->next[word[0] - 'a'] == NULL)return false; return search(word.substr(1), tmp->next[word[0] - 'a']); } };
0 0
- LeetCode *** 211. Add and Search Word - Data structure design(字典树)
- leetcode.211. Add and Search Word - Data structure design字典树
- leetcode Add and Search Word - Data structure design回溯算法+Trie字典树
- [leetcode] 211.Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- [Leetcode]211. Add and Search Word - Data structure design @python
- Leetcode 211. Add and Search Word - Data structure design
- 211. Add and Search Word - Data structure design LeetCode
- leetcode 211. Add and Search Word - Data structure design
- LeetCode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode-211.Add and Search Word - Data structure design
- Leetcode 211. Add and Search Word - Data structure design
- LeetCode-Add and Search Word - Data structure design-前缀树
- leetcode---Add and Search Word - Data structure design---Trie树
- LeetCode 211.Add and Search Word - Data structure design(trie树)
- 211.leetcode Add and Search Word - Data structure design(medium)[Trie树]
- 二维码扫描乱码的问题解决方法
- android中java与js通信(可以用html来做页面,进行交互)
- ubuntu14.04安装squirrelmail
- jQuery中ajax的使用和缓存问题解决
- Sql去除数据库中某一列的特殊字符
- LeetCode *** 211. Add and Search Word - Data structure design(字典树)
- iOS高级动画:圆形树展开&收起动画
- Openstack 中的zone ,aggregates和host及其应用
- COM技术入门基础知识
- 6个优秀的思维导图网站
- 堆栈问题
- 1040. Longest Symmetric String (25)
- 大脑“芯片”与地平线机器人科技创新企业
- 别问楼主是什么,自己看嘿嘿,mark