LeetCode(211) Add and Search Word - Data structure design
来源:互联网 发布:淘宝女装店铺素材 编辑:程序博客网 时间:2024/05/12 18:20
用递归来实现递推
class TrieNode {public: int val; TrieNode *children[26]; // Initialize your data structure here. TrieNode() { val = 0; for (int i=0; i < 26; i++) children[i] = NULL; }};class WordDictionary {public: WordDictionary() { root = new TrieNode(); } WordDictionary(TrieNode *p) { root = p; } // Adds a word into the data structure. void addWord(string word) { TrieNode *p = root; for(int i = 0; i < word.size(); i++) { if(p->children[word[i] - 'a'] == NULL) p->children[word[i] - 'a'] = new TrieNode(); p = p->children[word[i] - 'a']; } p->val = 1; } // 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() == 1) { TrieNode *p = root; if(word[0] != '.') { if(p->children[word[0] - 'a'] != NULL && (p->children[word[0] - 'a'])->val == 1) return true; return false; }else { for(int j = 0; j < 26; j++) { if(p->children[j] != NULL && (p->children[j])->val == 1) return true; } return false; } } TrieNode *p = root; if(word[0] != '.') { if(p->children[word[0] - 'a'] == NULL) return false; WordDictionary word3(p->children[word[0] - 'a']); return word3.search(word.substr(1)); }else { for(int j = 0; j < 26; j++) { if(p->children[j] != NULL) { WordDictionary word2(p->children[j]); if(word2.search(word.substr(1))) return true; } } return false; } }public: TrieNode* root;};// 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
- [Leetcode 211, Medium] 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-211-Add and Search Word - Data structure design
- leetcode:Trie:Add and Search Word - Data structure design(211)
- 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
- POJ 1503 Integer Inquiry 高精度
- SPOOLing系统是虚拟设备最典型SORT的代表
- zoj3344 第一类斯特林数+java大数
- 利用rsync+inotify搭建实时同步系统
- 信息化项目的高可用方案
- LeetCode(211) Add and Search Word - Data structure design
- 福建什么是加值电话 电话吸费代理 小投入高回报
- 三明回拨吸费手机 响一声电话吸费 电话群呼器
- 程序员如何快速准备面试中的算法
- 真的电话吸费 正规加盟招商 轻松奔小康
- hdu 1317 XYZZY (负权回路问题,SPAF实现)
- OC 内存管理的黄金法则
- 南平哪里加值电话 供应吸费电话 代理增值电话
- 不是技术牛人,如何拿到国内IT巨头的Offer