LeetCode Add and Search Word - Data structure design
2015-05-16 10:17
513 查看
题目
思路
前缀树,之前有一道LeetCode的题差不多的,我用之前的代码修改了一下即过。
代码
思路
前缀树,之前有一道LeetCode的题差不多的,我用之前的代码修改了一下即过。
代码
[code]struct WordDictionary { char c; // sons for "abcdefghijklmnopqrstuvwxyz\0" struct WordDictionary * son[27]; }; /** Initialize your data structure here. */ struct WordDictionary* wordDictionaryCreate() { struct WordDictionary * WordDictionary = (struct WordDictionary*)malloc(sizeof(struct WordDictionary)); WordDictionary->c = '\0'; memset(WordDictionary->son, 0, sizeof(WordDictionary->son)); return WordDictionary; } /** Inserts a word into the data structure. */ void addWord(struct WordDictionary * wordDictionary, char * word) { if (*word == '\0') { wordDictionary->son[26] = wordDictionaryCreate(); // notice that '\0' is important. // There's "abc\0" in Trie doesn't mean there's a word "ab\0". wordDictionary->son[26]->c = '\0'; return; } if (wordDictionary->son[*word - 'a'] == NULL) { wordDictionary->son[*word - 'a'] = wordDictionaryCreate(); wordDictionary->son[*word - 'a']->c = *word; addWord(wordDictionary->son[*word - 'a'], word + 1); } else { addWord(wordDictionary->son[*word - 'a'], word + 1); } } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ bool search(struct WordDictionary * wordDictionary, char * word) { if (*word == '\0') { if (wordDictionary->son[26] != NULL) return true; else return false; } if (*word == '.') { for (int i = 0; i < 26; i++) { if (wordDictionary->son[i] != NULL) { if (search(wordDictionary->son[i], word + 1)) return true; } } } else { if (wordDictionary->son[*word - 'a'] == NULL) { return false; } else { return search(wordDictionary->son[*word - 'a'], word + 1); } } } /** Deallocates memory previously allocated for the data structure. */ void wordDictionaryFree(struct WordDictionary * wordDictionary) { if (wordDictionary != NULL) { for (int i = 0; i < 26; i++) { wordDictionaryFree(wordDictionary->son[i]); } free(wordDictionary); } }
相关文章推荐
- 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 (Java)
- Leetcode 211. Add and Search Word - Data structure design
- [LeetCode] 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 211. Add and Search Word - Data structure design
- [Leetcode 211, Medium] 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 添加和查找单词-数据结构设计
- LeetCode-Add and Search Word - Data structure design
- LeetCode 211.Add and Search Word - Data structure design(trie树)
- 【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
- AddandSearchWordDatastructuredesign【LeetCode】
- Leetcode 211. Add and Search Word - Data structure design