zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    xml学习_上篇
    回首博客 年满一周年,记录90个点滴瞬间
    编写PHP代码总结
    代码高亮插件SyntaxHighlighter
    记忆,是一座沙城!
    dreamweaver中的 map怎么调用?_制作热点图像区域
    手机端rem如何适配_rem详解及使用方法
    彻底弄懂css中单位px和em,rem的区别
    JavaScript学习方法
    10条建议让你创建更好的jQuery插件
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12602719.html
Copyright © 2011-2022 走看看