博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 804. Unique Morse Code Words
阅读量:6305 次
发布时间:2019-06-22

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

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.

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.

题目描述:我们可以将单词描述为摩斯密码,问在一组单词中有多少个代表的莫斯密码是不同的。

题目分析:和 很类似,也是为了过滤掉重复的元素,我们可以用set集合去过滤。然后把单词转换为莫斯密码,然后过滤即可。

python 代码:

class Solution(object):    def uniqueMorseRepresentations(self, words):        """        :type words: List[str]        :rtype: int        """        maps = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]        words_length = len(words)        morse_code = set()        for i in range(words_length):            word_string = words[i]            word_string_length = len(word_string)            string_code = ''            for i in range(word_string_length):                string_code += maps[ord(word_string[i]) - 97]            morse_code.add(string_code)                    return len(morse_code)

C++ 代码:

class Solution {public:    int uniqueMorseRepresentations(vector
& words) { set
morse_code; vector
string_code(words.size()); vector
maps = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};// 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// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 for(int i = 0; i < words.size(); i++){ for(int j = 0; j < words[i].size(); j++){ string_code[i].append(maps[words[i][j] - 'a']); } } for(int i = 0; i < string_code.size(); i++){ morse_code.insert(string_code[i]); } return morse_code.size(); }};

转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/10158761.html

你可能感兴趣的文章
注册和上传文件(头像)
查看>>
使用OVS
查看>>
键盘回收的几种方法
查看>>
Python(条件判断和循环)
查看>>
day4 linux安装python
查看>>
LeetCode Container With Most Water (Two Pointers)
查看>>
vue (v-if show 问题)
查看>>
https基础
查看>>
css3 canvas之刮刮卡效果
查看>>
并查集模板
查看>>
RESTful Mongodb
查看>>
BZOJ3237:[AHOI2013]连通图(线段树分治,并查集)
查看>>
如何提高Ajax性能
查看>>
Android--自定义加载框
查看>>
LINUX下 lamp安装及配置
查看>>
BZOJ3105 [cqoi2013]新Nim游戏
查看>>
困惑的前置操作与后置操作
查看>>
SDNU 1269.整数序列(水题)
查看>>
BZOJ 2118 Dijkstra
查看>>
Go语言基础之结构体
查看>>