leetcode 676. Implement Magic Dictionary
原题:
Implement a magic directory with buildDict
, and search
methods.
For the method buildDict
, you'll be given a list of non-repetitive words to build a dictionary.
For the method search
, you'll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built.
Example 1:
Input: buildDict(["hello", "leetcode"]), Output: Null Input: search("hello"), Output: False Input: search("hhllo"), Output: True Input: search("hell"), Output: False Input: search("leetcoded"), Output: False
Note:
- You may assume that all the inputs are consist of lowercase letters
a-z
. - For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
- Please remember to RESET your class variables declared in class MagicDictionary, as static/class variables are persisted across multiple test cases. Please see here for more details.
代码如下:
typedef struct {char* string;int len;struct MagicDictionary* next;
} MagicDictionary;/** Initialize your data structure here. */
MagicDictionary* magicDictionaryCreate() {MagicDictionary* head;head=(MagicDictionary*)malloc(sizeof(MagicDictionary));head->next=NULL;return head;
}/** Build a dictionary through a list of words */
void magicDictionaryBuildDict(MagicDictionary* obj, char** dict) {if(dict==NULL||*dict==NULL)return;//这个地方写的太脑残了int dictLen=0;char** q=dict;while(*(q+dictLen)!=NULL){dictLen++;}printf("%d",dictLen);for(int n=0;n<dictLen;n++){MagicDictionary* p;p=(MagicDictionary*)malloc(sizeof(MagicDictionary));int stringLen=strlen(*dict+n);p->string=(char*)malloc(sizeof(char)*(stringLen+1));strcpy(p->string,*(dict+n));p->len=stringLen;printf("%s",p->string);p->next=obj->next;obj->next=p;}
}/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool magicDictionarySearch(MagicDictionary* obj, char* word) {int wordLen=strlen(word);MagicDictionary* p=obj->next;while(p!=NULL){if(p->len!=wordLen){p=p->next;}else{int diff=0;for(int n=0;n<p->len;n++){if(*(word+n)!=*(p->string+n))diff++;}if(diff==1)return true;else{p=p->next;}}}return false;
}void magicDictionaryFree(MagicDictionary* obj) {MagicDictionary* p=obj->next;free(obj);while(p!=NULL){free(p->string);MagicDictionary* temp=p;p=temp->next;free(temp);}
}/*** Your MagicDictionary struct will be instantiated and called as such:* struct MagicDictionary* obj = magicDictionaryCreate();* magicDictionaryBuildDict(obj, dict);* bool param_2 = magicDictionarySearch(obj, word);* magicDictionaryFree(obj);*/
我只能说leetcode对c太不友好了,欺负我们数据结构没有对应的属性或者函数是吧。。。
你tm让我怎么找二维数组的一维长度。。。。
难受。。。 搜了半天也没有好办法。
leetcode 676. Implement Magic Dictionary相关推荐
- leetcode 676. Implement Magic Dictionary | 676. 实现一个魔法字典(DFS+Trie 前缀树)
题目 https://leetcode.com/problems/implement-magic-dictionary/description/ 题解 题意理解 前缀树问题,大意是是让你在字典中找到是 ...
- LeetCode 676. 实现一个魔法字典
文章目录 一.题目 1.题目描述 2.基础框架 3.原题链接 二.解题报告 1.思路分析 2.时间复杂度 3.代码详解 三.本题小知识 四.加群须知 一.题目 1.题目描述 设计一个使用单词列表进 ...
- LeetCode 225. Implement Stack using Queues--用队列实现栈--C++解法
LeetCode 225. Implement Stack using Queues–用队列实现栈–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在 ...
- LeetCode 232. Implement Queue using Stacks--用2个栈来实现一个队列--C++解法
LeetCode 232. Implement Queue using Stacks–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大 ...
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- LeetCode 676. 实现一个魔法字典(哈希)
1. 题目 实现一个带有buildDict, 以及 search方法的魔法字典. 对于buildDict方法,你将被给定一串不重复的单词来构建一个字典. 对于search方法,你将被给定一个单词,并且 ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- LeetCode - 28. Implement strStr()
28. Implement strStr() Problem's Link -------------------------------------------------------------- ...
- leetcode 208. Implement Trie (Prefix Tree) | 208. 实现 Trie 前缀树(Java)
题目 https://leetcode.com/problems/implement-trie-prefix-tree/ 题解 第一版:暴力法 import java.util.LinkedHashS ...
最新文章
- .Net 引用命名空间
- 全球及中国自动驾驶行业应用领域及投资前景展望报告2022-2028年版
- Linux编程 9 (shell类型,shell父子关系,子shell用法)
- 2016陕西省ACM 热身体B 种类并查集
- Ubuntu下将Sublime Text设置为默认编辑器
- 本地如何搭建IPv6环境测试你的APP
- MSSQL如何在没有主键的表中删除重复数据
- [Leedcode][JAVA][面试题 16.03. 交点]
- 查看oracle资源使用情况,Oracle查询表空间使用情况
- 多系统重装其中Win7后的启动引导列表恢复
- 猜数游戏c语言编程while,【游戏编程】猜数字游戏(C语言)
- UiPath安装pdf
- 台式计算机usb口不识别鼠标,电脑USB接口不能识别鼠标键盘怎么办
- 排队服务系统仿真研究计算机模拟,基于蒙特卡洛方法排队系统性能的仿真优化研究...
- latex如何插入图片格式
- 如何培养自己的商业思维能力?
- js 树状图数组批量循环操作
- 设定快搜Caption时注意
- Win7系统不同程序无法同时播放声音是怎么回事?
- 【Python】np.polyfit点拟合曲线