经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++实现LeetCode(211.添加和查找单词-数据结构设计)
来源:jb51  时间:2021/8/9 15:17:25  对本文有异议

[LeetCode] 211.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.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.

LeetCode出新题的速度越来越快了,有点跟不上节奏的感觉了。这道题如果做过之前的那道 Implement Trie (Prefix Tree) 实现字典树(前缀树)的话就没有太大的难度了,还是要用到字典树的结构,唯一不同的地方就是search的函数需要重新写一下,因为这道题里面'.'可以代替任意字符,所以一旦有了'.',就需要查找所有的子树,只要有一个返回true,整个search函数就返回true,典型的DFS的问题,其他部分跟上一道实现字典树没有太大区别,代码如下:

  1. class WordDictionary {
  2. public:
  3. struct TrieNode {
  4. public:
  5. TrieNode *child[26];
  6. bool isWord;
  7. TrieNode() : isWord(false) {
  8. for (auto &a : child) a = NULL;
  9. }
  10. };
  11. WordDictionary() {
  12. root = new TrieNode();
  13. }
  14. // Adds a word into the data structure.
  15. void addWord(string word) {
  16. TrieNode *p = root;
  17. for (auto &a : word) {
  18. int i = a - 'a';
  19. if (!p->child[i]) p->child[i] = new TrieNode();
  20. p = p->child[i];
  21. }
  22. p->isWord = true;
  23. }
  24.  
  25. // Returns if the word is in the data structure. A word could
  26. // contain the dot character '.' to represent any one letter.
  27. bool search(string word) {
  28. return searchWord(word, root, 0);
  29. }
  30. bool searchWord(string &word, TrieNode *p, int i) {
  31. if (i == word.size()) return p->isWord;
  32. if (word[i] == '.') {
  33. for (auto &a : p->child) {
  34. if (a && searchWord(word, a, i + 1)) return true;
  35. }
  36. return false;
  37. } else {
  38. return p->child[word[i] - 'a'] && searchWord(word, p->child[word[i] - 'a'], i + 1);
  39. }
  40. }
  41. private:
  42. TrieNode *root;
  43. };
  44.  
  45. // Your WordDictionary object will be instantiated and called as such:
  46. // WordDictionary wordDictionary;
  47. // wordDictionary.addWord("word");
  48. // wordDictionary.search("pattern");

讨论:这道题有个很好的Follow up,就是当搜索的单词中存在星号怎么搞,星号的定义和Wildcard Matching中一样,可以代表任意的字符串,包括空字符串,请参见评论区1楼。

类似题目:

Implement Trie (Prefix Tree)

Wildcard Matching

参考资料:

https://leetcode.com/discuss/36246/my-java-trie-based-solution

到此这篇关于C++实现LeetCode(211.添加和查找单词-数据结构设计)的文章就介绍到这了,更多相关C++实现添加和查找单词-数据结构设计内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号