LeetCode 804 Unique Morse Code Words--python,java解法
发布日期:2022-03-18 18:19:25 浏览次数:6 分类:技术文章

本文共 2830 字,大约阅读时间需要 9 分钟。

题目地址:



Difficulty:easy

Acceptance:74.1%


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.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

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.

Example:Input: words = ["gin", "zen", "gig", "msg"]Output: 2Explanation: The transformation of each word is:"gin" -> "--...-.""zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."There are 2 different transformations, "--...-." and "--...--.".

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

这题的意思是我们将单词转化为摩斯密码,问在一组单词中有多少个不同的莫斯密码。

使用集合去重就可以了。


Python3代码如下:

class Solution:    def uniqueMorseRepresentations(self, words: List[str]) -> int:        maps=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]        morse_code = set()        for i in words:            temp=""            for j in i:                temp+=maps[ord(j)-97]            morse_code.add(temp)        return len(morse_code)

官方标准解法:

class Solution(object):    def uniqueMorseRepresentations(self, words):        MORSE = [".-","-...","-.-.","-..",".","..-.","--.",                 "....","..",".---","-.-",".-..","--","-.",                 "---",".--.","--.-",".-.","...","-","..-",                 "...-",".--","-..-","-.--","--.."]        seen = {
"".join(MORSE[ord(c) - ord('a')] for c in word) for word in words} return len(seen)

官方的Java解法:

class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] MORSE = new String[]{
".-","-...","-.-.","-..",".","..-.","--.", "....","..",".---","-.-",".-..","--","-.", "---",".--.","--.-",".-.","...","-","..-", "...-",".--","-..-","-.--","--.."}; Set
seen = new HashSet(); for (String word: words) {
StringBuilder code = new StringBuilder(); for (char c: word.toCharArray()) code.append(MORSE[c - 'a']); seen.add(code.toString()); } return seen.size(); }}

转载地址:https://zhang0peter.blog.csdn.net/article/details/88225723 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:最大乘积
下一篇:LeetCode 965 Univalued Binary Tree--判断二叉树的所有节点的值是否相同--python,java解法

发表评论

最新留言

不错!
[***.144.177.141]2024年04月24日 00时57分34秒