LeetCode-211.Add and Search Word - Data structure design
来源:互联网 发布:人工智能危机作文 编辑:程序博客网 时间:2024/05/27 10:42
https://leetcode.com/problems/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") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
关于Trie树的构造可以查看题 LeetCode-208.Implement Trie (Prefix Tree)
查找".ad"和"b.."等非完整词时,需要用到递归
class TrieNode{public:TrieNode *children[26];bool isWord;TrieNode(){isWord = false;for (auto &node : children)node = NULL;}};class WordDictionary {public: WordDictionary(){root = new TrieNode();} // Adds a word into the data structure. void addWord(string word) { TrieNode *node = root;for (char c : word){if (!node->children[c - 'a'])node->children[c - 'a'] = new TrieNode();node = node->children[c - 'a'];}node->isWord = 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 search2(word, root); } private:TrieNode* root;bool search2(string word, TrieNode *pp){TrieNode *node = pp;for (char c : word){if (c == '.'){for (auto &p : node->children){if (!p)continue;if (search2(word.substr(1), p))return true;}return false;}else{if (!node->children[c - 'a'])return false;return search2(word.substr(1), node->children[c - 'a']);}}return node->isWord;}};// Your WordDictionary object will be instantiated and called as such:// WordDictionary wordDictionary;// wordDictionary.addWord("word");// wordDictionary.search("pattern");
0 0
- [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 211 - Add and Search Word - Data structure design
- LeetCode Add and Search Word - Data structure design
- Leetcode Add and Search Word - Data structure design
- leetcode:Add and Search Word - Data structure design
- [leetcode]Add and Search Word - Data structure design
- [LeetCode] Add and Search Word - Data structure design
- leetcode #211 Add and Search Word - Data structure design
- CentOS7安装iptables防火墙
- 泛型(二)之通配符
- Java IO中转换流的作用
- 西二旗完全装逼指南
- STL vector用法介绍
- LeetCode-211.Add and Search Word - Data structure design
- javaEE面试【1】(java反射机制)
- Linux蓝牙系统(3) Linux 命令
- Andorid SDK 国内镜像
- HTML5的FileReader API在浏览器中一行行的读取本地文本文件
- map 按照输入顺序输出
- [JZOJ4637] 大鱼海棠(By--A_Crazy_Czy)
- 自己编写代码实现常用字符串函数的功能
- Storm之——配置项详解