LeetCode804

  本题主要注意python中集合的定义、添加、取大小,以及单个字符转数值的处理。 

import numpy as np

def uniqueMorseRepresentations(words):
    z = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    s = set()
    for str in words:
        t = ""
        for c in str:
            t += z[ord(c)-ord('a')]   # ord将长度为1的字符串转为ASCII数值,逆操作如:chr(97)得到'a'
    s.add(t)
    return len(s)

# sample:
words = ["gin", "zen", "gig", "msg"]
print(uniqueMorseRepresentations(words))

猜你喜欢

转载自blog.csdn.net/zuocuomiao/article/details/82053237