zoukankan      html  css  js  c++  java
  • leetcode摩尔斯密码

    国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。

    为了方便,所有26个英文字母对应摩尔斯密码表如下:

    [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。

    返回我们可以获得所有词不同单词翻译的数量。

    例如:
    输入: words = ["gin", "zen", "gig", "msg"]
    输出: 2
    解释:
    各单词翻译如下:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."

    共有 2 种不同翻译, "--...-." 和 "--...--.".
    解答:
    class Solution:
    def uniqueMorseRepresentations(self, words: List[str]) -> int:
    m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    abc = [chr(i) for i in range(97,123)]
    z = dict(zip(abc, m))
    res=[]
    # print(z['a'])
    for i in range(len(words)):
    temp = words[i]
    x = ""
    for j in range(len(temp)):
    x += z[str(temp[j])]
    if x not in res:
    res.append(x)
    return(len(res))

  • 相关阅读:
    轮播 margin-left实现
    点击按钮切换图片
    运用把不同的方式排版,涉及到float box-flox box-orient
    chrome中font-size<12px时并不更改字体大小仍未12px
    js实现跑马灯
    支付宝支付集成
    前端技术博客
    在iphone5/5s出现界面显示不全,大小为iphone4/4s 的问题
    UIImage使用总结
    在IOS开发中使用自定义的字体
  • 原文地址:https://www.cnblogs.com/qwksjy/p/11252739.html
Copyright © 2011-2022 走看看