当前位置: 首页> 文旅> 文化 > C语言 | Leetcode C语言题解之第211题添加与搜索单词-数据结构设计

C语言 | Leetcode C语言题解之第211题添加与搜索单词-数据结构设计

时间:2025/8/29 23:46:28来源:https://blog.csdn.net/m0_59237910/article/details/140141225 浏览次数:0次

题目:

题解:

typedef struct WordDictionary_tag{struct WordDictionary_tag *next[26];bool finish;
} WordDictionary;/** Initialize your data structure here. */WordDictionary* wordDictionaryCreate() {WordDictionary *wd = (WordDictionary*) malloc(sizeof(WordDictionary));memset(wd->next, 0, sizeof(wd->next));wd->finish = false;return wd;
}void wordDictionaryAddWord(WordDictionary* obj, char * word) {for (int i = 0; word[i]; i++){if (obj->next[word[i] - 'a']){obj = obj->next[word[i] - 'a'];continue;}obj->next[word[i] - 'a'] = (WordDictionary*) malloc(sizeof(WordDictionary));obj = obj->next[word[i] - 'a'];memset(obj->next, 0, sizeof(obj->next));obj->finish = false;}obj->finish = true;
}bool wordDictionarySearch(WordDictionary* obj, char * word) {if (!obj){return false;}if (!word[0]){return obj->finish;}if (word[0] != '.'){return wordDictionarySearch(obj->next[word[0] - 'a'], word + 1);}for (int i = 0; i < 26; i++){if (wordDictionarySearch(obj->next[i], word + 1)){return true;}}return false;
}void wordDictionaryFree(WordDictionary* obj) {for (int i = 0; i < 26; i++){if (obj->next[i]){wordDictionaryFree(obj->next[i]);}}free(obj);
}
关键字:C语言 | Leetcode C语言题解之第211题添加与搜索单词-数据结构设计

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: