题目描述 LeetCode 804
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations, "--...-." and "--...--.".
Note:
The length of words will be at most 100.
Each words[i] will have length in range [1, 12].
words[i] will only consist of lowercase letters.
中文描述
题目以摩尔斯电码(Morse Code)为背景设题,摩尔斯电码简单来说是把26个小写英文字母( 'a' ~ 'z' ) ,为它们建立了一一映射,从题目中所说,'a' -> '.-' , 'b' -> '-...' , -- , 'z' -> '--..' .
本文的意思是根据每个字母的摩尔斯电码,输入为字符串数组,为每个字符串映射出相应的电码,并且要去掉重复的,最终返回这个字符串数组所映射成的不同的电码个数。
解题思路
- 输入字符串读入 word 指针数组
- 摩尔斯每个字符的映射字符串读入 wordsList 数组。
- 三重 for 循环,第一重从 word 中读取字符串,第二重依次读取从第一重循环中读到的字符串的单个字符,第三重将从第二重读到的单个字符映射为 wordsList 中该字符对应的摩尔斯电码,最终依次将 wordsList 中的映射依次读入 key[i] 数组。
- a[] 用于计数,因题目要求去除冗余,所以,我先设置 a[i] 全为 1,从第一个依次往后都,如果后面和前面有重复的,就把 a[i] 设置为 0,最终 a[] 数组中为 1 的就是去掉重复的摩尔斯电码。(简单来说,可以把 a[] 数组理解为 key[] 的标签)
C语言实现
# include
# include
int uniqueMorseRepresentations(char** words, int wordsSize)
{
char wordsList[26][12] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int i = 0;
int j = 0;
int k = 0;
char key[100][100];
int size = 0;
int a[100];
// 把 words 中的每个字符串,找到字符串的每个字符在 wordsList 中的映射,把每个字符映射写入 key[] 数组
for ( i = 0; i < wordsSize; i ++)
{
size = 0;
for ( j = 0; j < strlen( *(words + i) ); j ++)
{
//printf("%c ", *(*(words + i) + j));
//printf("%s ", wordsList[ *(*(words + i) + j) - 'a' ]);
for ( k = 0; k < strlen(wordsList[ *(*(words + i) + j) - 'a' ]); k ++)
{
key[i][size++] = wordsList[ *(*(words + i) + j) - 'a' ][k];
}
}
key[i][size] = '\0';
//printf("%d %s\n\n", i, key[i]);
}
// a[] 数组初始化全为 1 , 用于打标签
for (i = 0; i < wordsSize; i ++)
{
a[i] = 1;
}
// 去除冗余,如果发现相应有冗余,如 key[i] 和 key[j] 冗余,则将 a[j] 设为 0
for (i = 0; i < wordsSize - 1; i ++)
{
if(a[i] == 1)
{
for (j = i + 1; j < wordsSize; j ++)
{
if( strcmp(key[i], key[j]) == 0)
{
a[j] = 0;
}
}
}
}
// 计数,最终在 a[] 数组中,为 1 的元素都是独一无二的
size = 0;
for(i = 0; i < wordsSize; i ++)
{
if(a[i] == 1)
{
size ++ ;
}
}
return size;
}
main()
{
char *words[4] = {"gin", "zen", "gig", "msg"};
printf("%d\n\n", uniqueMorseRepresentations(words, 4));
}
注意
这道题总体思考没问题,代码也在 LeetCode执行通过,但是在本地执行会用问题,估计是因为指针吧,我看编译器的报错,貌似要求 uniqueMorseRepresentations 这个函数,输入为指针,输出也要为指针,我真是 ***,当然了,可能我才疏学浅,也可能是编译器的原因,欢迎大牛指正。- 对第一段更正, 经过思考,终于发现错误所在,真是基础不牢,地动山摇
我的错误是,最近写 python 比较多,把 python 的输出语法理所当然地用到了 C 语言上
python 中print(666)
是没问题的。
但是 在 C 语言中必须是 'printf("%d", 666)' ,不能直接printf(666) - 代码已更正,本地可直接运行。
吐槽
- C语言太不友好,我用C竟然能写 50多行,在网上看别人用 C++ 才几行,真是心哇凉哇凉的。 最近发现是 C 语言库函数少的原因,就当锻炼编程能力了。
- 以后可能会转战 C++ 啦,C 的指针真是博大精深,不过后话啦,据说 C 执行效率最快,java 最慢。
- 网上有人建议,每做完一道题目去讨论区看看高赞代码,查漏补缺,不过现在真没时间,等以后临近找工作,再去看别人的代码思考吧。