LeetCode 211.Add and Search Word - Data structure design(trie树)
来源:互联网 发布:华夏免费版服务端源码 编辑:程序博客网 时间:2024/05/06 20:40
题意:创建一个单词库,支持两种操作,给库里面加入新单词和查询某单词是否在库中,但是注意用’.’来做通配符,可以代替任意字符.
分析:trie树的改造.主要是通配符的处理.这里把搜索函数处理了一下,用递归实现,当配件通配符号的时候遍历它的所有26个分枝来求满足条件的.
code:
class WordDictionary {private: struct node { int have; node *next[26]; node(void) { have = 0; for (int i = 0; i < 26; i++) next[i] = NULL; } };public: node *root = new node; // Adds a word into the data structure. // The time consumer is O(n) void addWord(string word) { node *p = root; for (int i = 0; i < word.size(); i++) { int aim = word[i] - 'a'; if ((p -> next)[aim] == NULL) (p -> next)[aim] = new node; p = (p -> next)[aim]; } (p -> have) = (p -> have) + 1; } bool can(node *p, int head, const string &word) { if (head == word.size() && p -> have) return true; int aim = word[head] == '.' ? 26 : word[head] - 'a'; if (aim == 26) { for (int i = 0; i < 26; i++) if ((p -> next)[i] != NULL && can( (p -> next)[i], head + 1, word)) return true; return false; } if ((p -> next)[aim] != NULL) return can((p -> next)[aim], head + 1, word); return false; } // 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) { node *p = root; int head = 0; return can(p, head, word); }};// Your WordDictionary object will be instantiated and called as such:// WordDictionary wordDictionary;// wordDictionary.addWord("word");// wordDictionary.search("pattern");
0 0
- leetcode---Add and Search Word - Data structure design---Trie树
- LeetCode Add and Search Word - Data structure design Trie
- leetcode:Trie:Add and Search Word - Data structure design(211)
- LeetCode 211.Add and Search Word - Data structure design(trie树)
- 211.leetcode Add and Search Word - Data structure design(medium)[Trie树]
- 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-前缀树
- 【AngularJS】——核心特性之路由
- Ugly Number---263
- F——RGCDQ (HDU 5317)
- 改变iOS系统自带返回按钮事件
- 在数组中选择k
- LeetCode 211.Add and Search Word - Data structure design(trie树)
- 1006-贪心算法acm
- 参观百度有感
- C语言编写基于mysql数据库的简单数据管理系统
- 创建了一个自己的博客git
- tomcat监控工具
- 第三周练习-3 递归求最大公约数
- 记录成长之路一
- github 开源代码托管