编程语言
首页 > 编程语言> > python leetcode 唯一摩尔斯密码词【简单题】

python leetcode 唯一摩尔斯密码词【简单题】

作者:互联网

1.读懂题目

2. 分析,推导解法,产生思路。

解题思路:

set集合元素不重复。ord():将字符转换成对应的 ASCII 数值,或者 Unicode 数值

3.代码实现

class Solution(object):
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        # ord():将字符转换成对应的 ASCII 数值,或者 Unicode 数值
        # set集合元素不重复。
        dict = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        words_set ={''.join(dict[ord(c)-ord('a')] for c in word) for word in words}
        return len(words_set)

 

标签:set,word,python,摩尔,数值,dict,words,ord,leetcode
来源: https://blog.csdn.net/qq_37974982/article/details/114557218