LeetCode 804 Unique Morse Code Words 解题报告

题目要求

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-""b" maps to "-...""c" maps to "-.-.", and so on.

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We‘ll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

题目分析及思路

题目给出摩斯码的编码规则,要求返回所有单词不同transformation的个数。找出多少种不同的情况,可以用len(set())的方式进行处理。摩斯码和字母的对应关系可以用字典。

python代码?

class Solution:

def uniqueMorseRepresentations(self, words):

"""

:type words: List[str]

:rtype: int

"""

morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

letter = [‘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‘]

mldict = dict(zip(letter,morse))

res = set()

for word in words:

mword = ""

for w in word:

mword = mword + mldict[w]

res.add(mword)

return len(res)

原文地址:https://www.cnblogs.com/yao1996/p/10212454.html

时间: 2024-10-08 19:16:39

LeetCode 804 Unique Morse Code Words 解题报告的相关文章

LeetCode 804. Unique Morse Code Words

804. Unique Morse Code Words International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to &

Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to &q

[LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a"maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on. Fo

Unique Morse Code Words

Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1)problem International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to

[LeetCode] Unique Morse Code Words 独特的摩斯码单词

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on. F

7. Unique Morse Code Words

Title: International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and s

LeetCode: Search in Rotated Sorted Array 解题报告

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想 更新下:这道突然就很多访问,想起来好像是lt提交通过率最低的,嗯,我写的也不是特别详细,如有问题可以新浪微博@MebiuW交流~~ 这道题条条框框是在太多了,各种情况..不过简略来说,正确的做法应该是: 1.熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则. 2.对输入的数字首先进行必要的检测,是否有abc或者中间空格等非法字符 3.将e前面和e后面分开计算!e前面允许有小数点形式的,e后面不允许有小数点形式的 4.数字的形式一般是 可以有正负号