• 1013. 独特的摩尔斯编码


    1013. 独特的摩尔斯编码

    中文English

    摩尔斯电码定义了一种标准编码,把每个字母映射到一系列点和短划线,例如:a -> .-b -> -...c ->-.-.

    给出26个字母的完整编码表格:

    [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    

    现在给定一个单词列表,每个单词中每个字母可以写成摩尔斯编码。 例如,cab可以写成-.-.-....-,(把c,a,b的莫尔斯编码串接起来)。 我们称之为一个词的转换。

    返回所有单词中不同变换的数量。

    样例

    样例1:

    输入: words = ["gin", "zen", "gig", "msg"]
    输出: 2
    解释: 
    每一个单词的变换是:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."
    
    这里有两种不同的变换结果: "--...-."和"--...--.".
    

    样例2:

    输入: words = ["a", "b"]
    输出: 2
    解释: 
    每一个单词的变换是:
    "a" -> ".-"
    "b" -> "-..."
    这里有两种不同的变换结果:".-" and "-...".
    

    注意事项

    • 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.
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param words: the given list of words
        @return: the number of different transformations among all words we have
        """
        '''
        大致思路:
        1.初始化count = 0(计数,计算出不同的摩斯密码的个数),res = []。需要给出与摩斯密码映射字典
        2.循环words,s_res = '',每个字符串进行循环拼接s_res,如果不存在res里面的话,则append进来,最终返回res的长度即可
        '''
        def uniqueMorseRepresentations(self,words):
            count,res = 0,[]
            morse_dic = self.return_morse_dic()
    
            for column in words:
                s_res = ''
                for i in column:
                    s_res += morse_dic[i]
                if s_res not in res:
                    res.append(s_res)
            return len(res)
        
        def return_morse_dic(self):
            a = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
            s = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
            dic = {}
            count = 0
            for i in s:
                dic[i] = a[count]
                count += 1
            return dic
  • 相关阅读:
    Day3-B-Round Marriage CodeForces-981F
    Day3-A-Problem H. Monster Hunter HDU6326
    Day3-G
    Day3-I-Squares POJ2002
    Day3-M-Cable master POJ1064
    Day3-N
    Day3-O-Median POJ3579
    Day3-P
    Day3-L-Cup HDU2289
    LeetCode "Majority Element"
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12602719.html
Copyright © 2020-2023  润新知