leetcode 804. Unique Morse Code Words

本题用到Python基本数据类型set,set是一个无序且不重复的元素集合。

class Solution(object):
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        Morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
                 "---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

        res = set()

        for i in range(len(words)):
            str=""
            for j in range(len(words[i])):
                str = str + Morse[ord(words[i][j])-ord('a')]
            res.add(str)
        return len(res)

猜你喜欢

转载自blog.csdn.net/cainiaohudi/article/details/79875870