博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
阅读量:5752 次
发布时间:2019-06-18

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

参考: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 "-...""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, "cab" 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.

翻译:是使用.-来表示字母,如果直接将字符串转换成摩尔斯电码,而没有空格的话,那么不同的字符串可能有相同的摩尔斯电码,下面给出一系列字符串,给出这些字符串能够得到多少种不同类的摩尔斯电码。

思路:

    1.首先根据字母拼接对应的摩尔斯电码,然后将这一段电码做hash映射,可以看做是一段01串,直接转化成二进制即可(可能溢出int范围,但是没有关系,相同的字符串溢出后也是一样的)

    2.将一系列字符串对应的hash值排序,去重即可。

代码:

class Solution {  public:      vector
mos={
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; string to_mos(string &word) { string ans=""; for(int i=0;i
& nums) { int n=nums.size(); if(n < 2) return n; int id = 1; for(int i = 1; i < n; i++) if(nums[i] != nums[i-1]) nums[id++] =nums[i]; return id; } int hash(string &m) { int ans=0; for(int i=0;i
& words) { int n=words.size(); vector
mos_words(n); for(int i=0;i
mos_words_hash(n,0); for(int i=0;i

 

转载于:https://www.cnblogs.com/zb-ml/p/8735314.html

你可能感兴趣的文章
模拟创建单向链表
查看>>
日志分析大致流程
查看>>
2018年5月9日第2课——RHEL7.0安装、登录、密码重置
查看>>
百度AI开放平台,共建AI生态
查看>>
传统习俗-小年主题PPT
查看>>
性别偏见不存在的,女性入行大数据也能有新未来
查看>>
Java 求1-100以内的所有素数,判断一个数是不是素数
查看>>
callback Promise async await 异步回调 案例
查看>>
yum
查看>>
windows server 2008 R2 AD 域之---省心省力的漫游配置文件①
查看>>
为什么a标签中使用img后的高度多了几个像素?
查看>>
域环境下禁止文件复制的方法
查看>>
AGAL学习笔记(二)
查看>>
如何使用Percona Data Recovery Tool恢复mysql 被delete的数据
查看>>
3天看完了《收获,不止Oracle》
查看>>
Cisco 2950 交换机 IP-MAC地址 绑定配置详细说明
查看>>
我的友情链接
查看>>
Mongodb学习(安装篇):在Window下安装
查看>>
如何为windows编译启用pdb支持
查看>>
if .. else 012
查看>>