LeetCode 804. Unique Morse Code Words

題目描述 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)為背景設(shè)題,摩爾斯電碼簡(jiǎn)單來(lái)說(shuō)是把26個(gè)小寫英文字母( 'a' ~ 'z' ) ,為它們建立了一一映射,從題目中所說(shuō),'a' -> '.-' , 'b' -> '-...' , -- , 'z' -> '--..' .
本文的意思是根據(jù)每個(gè)字母的摩爾斯電碼,輸入為字符串?dāng)?shù)組,為每個(gè)字符串映射出相應(yīng)的電碼,并且要去掉重復(fù)的,最終返回這個(gè)字符串?dāng)?shù)組所映射成的不同的電碼個(gè)數(shù)。

解題思路

  • 輸入字符串讀入 word 指針數(shù)組
  • 摩爾斯每個(gè)字符的映射字符串讀入 wordsList 數(shù)組。
  • 三重 for 循環(huán),第一重從 word 中讀取字符串,第二重依次讀取從第一重循環(huán)中讀到的字符串的單個(gè)字符,第三重將從第二重讀到的單個(gè)字符映射為 wordsList 中該字符對(duì)應(yīng)的摩爾斯電碼,最終依次將 wordsList 中的映射依次讀入 key[i] 數(shù)組。
  • a[] 用于計(jì)數(shù),因題目要求去除冗余,所以,我先設(shè)置 a[i] 全為 1,從第一個(gè)依次往后都,如果后面和前面有重復(fù)的,就把 a[i] 設(shè)置為 0,最終 a[] 數(shù)組中為 1 的就是去掉重復(fù)的摩爾斯電碼。(簡(jiǎn)單來(lái)說(shuō),可以把 a[] 數(shù)組理解為 key[] 的標(biāo)簽)

C語(yǔ)言實(shí)現(xiàn)

# include <stdio.h>
# include <string.h>

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 中的每個(gè)字符串,找到字符串的每個(gè)字符在 wordsList 中的映射,把每個(gè)字符映射寫入 key[] 數(shù)組
    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[] 數(shù)組初始化全為 1 , 用于打標(biāo)簽
    for (i = 0; i < wordsSize; i ++)
    {
        a[i] = 1;
    }
    // 去除冗余,如果發(fā)現(xiàn)相應(yīng)有冗余,如 key[i] 和 key[j] 冗余,則將 a[j] 設(shè)為 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;
                }
            }
        }
    }
        
    // 計(jì)數(shù),最終在 a[] 數(shù)組中,為 1 的元素都是獨(dú)一無(wú)二的
    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));
}

注意

  • 這道題總體思考沒(méi)問(wèn)題,代碼也在 LeetCode執(zhí)行通過(guò),但是在本地執(zhí)行會(huì)用問(wèn)題,估計(jì)是因?yàn)橹羔槹?,我看編譯器的報(bào)錯(cuò),貌似要求 uniqueMorseRepresentations 這個(gè)函數(shù),輸入為指針,輸出也要為指針,我真是 ***,當(dāng)然了,可能我才疏學(xué)淺,也可能是編譯器的原因,歡迎大牛指正。
  • 對(duì)第一段更正, 經(jīng)過(guò)思考,終于發(fā)現(xiàn)錯(cuò)誤所在,真是基礎(chǔ)不牢,地動(dòng)山搖
    我的錯(cuò)誤是,最近寫 python 比較多,把 python 的輸出語(yǔ)法理所當(dāng)然地用到了 C 語(yǔ)言上
    python 中 print(666) 是沒(méi)問(wèn)題的。
    但是 在 C 語(yǔ)言中必須是 'printf("%d", 666)' ,不能直接 printf(666)
  • 代碼已更正,本地可直接運(yùn)行。

吐槽

  • C語(yǔ)言太不友好,我用C竟然能寫 50多行,在網(wǎng)上看別人用 C++ 才幾行,真是心哇涼哇涼的。 最近發(fā)現(xiàn)是 C 語(yǔ)言庫(kù)函數(shù)少的原因,就當(dāng)鍛煉編程能力了。
  • 以后可能會(huì)轉(zhuǎn)戰(zhàn) C++ 啦,C 的指針真是博大精深,不過(guò)后話啦,據(jù)說(shuō) C 執(zhí)行效率最快,java 最慢。
  • 網(wǎng)上有人建議,每做完一道題目去討論區(qū)看看高贊代碼,查漏補(bǔ)缺,不過(guò)現(xiàn)在真沒(méi)時(shí)間,等以后臨近找工作,再去看別人的代碼思考吧。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容