Lion X

** LeetCode——804** <Excerpt in index | 首页摘要>
LeetCode——804——题目分析与C语言实现

<The rest of contents | 余下全文>

LeetCode_804_Unique Morse Code Words

实现步骤

1.建立Morse对照数组
2.声明一个数组用于存放一个字符串的Morse对应编码
3.声明一个数组用于存放重复的Morse码
4.将字符串逐个转化为Morse码
5.将字符串的Morse码与数组中的每一个Morse码对比,如果相同则退出比较,如果该Morse码不存在与数组中,则将该Morse码添加至该数组

代码实现(C语言)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int uniqueMorseRepresentations(char ** words, int wordsSize){
char morse[][5]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int i, j, k, find, count = 0;
char string[100];
char unique[100][100] = {""};

for(i = 0; i < wordsSize; i++){
j = 0;
strcpy(string, "");
while(words[i][j]){
strcat(string, morse[words[i][j++] - 'a']);
}

find = 0;
for(k = 0; k < count; k++){
if(strcmp(string, unique[k]) == 0){
find = 1;
}
}

if(!find){
strcpy(unique[count++], string);
}
}
return count;
}

 Comments