参考: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: 2
Explanation:
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<string> mos={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
string to_mos(string &word)
{
string ans="";
for(int i=;i<word.size();i++)
ans += mos[word[i]-'a'];
return ans;
}
int removeDuplicates(vector<int>& nums)
{
int n=nums.size();
if(n < ) return n;
int id = ;
for(int i = ; i < n; i++)
if(nums[i] != nums[i-])
nums[id++] =nums[i];
return id;
}
int hash(string &m)
{
int ans=;
for(int i=;i<m.size();i++)
{
if(m[i]=='-') ans++;
ans*=;
}
return ans;
}
int uniqueMorseRepresentations(vector<string>& words)
{
int n=words.size();
vector<string> mos_words(n);
for(int i=;i<n;i++)
mos_words[i] = to_mos(words[i]); vector<int> mos_words_hash(n,);
for(int i=;i<n;i++)
mos_words_hash[i] = hash(mos_words[i]); sort(mos_words_hash.begin(),mos_words_hash.end());
return removeDuplicates(mos_words_hash);
}
};

Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题的更多相关文章

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

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  2. LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)

    题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...

  3. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  4. LeetCode - 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  5. LeetCode 804 Unique Morse Code Words 解题报告

    题目要求 International Morse Code defines a standard encoding where each letter is mapped to a series of ...

  6. [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  7. 804. Unique Morse Code Words - LeetCode

    Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...

  8. 【Leetcode_easy】804. Unique Morse Code Words

    problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...

  9. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

随机推荐

  1. C语言描述链表的实现及操作

    一.链表的创建操作 // 操作系统 win 8.1 // 编译环境 Visual Stuido 2017 #include<stdio.h> #include<malloc.h> ...

  2. 【ASP.NET Core】如何隐藏响应头中的 “Kestrel”

    全宇宙人民都知道,ASP.NET Core 应用是不依赖服务器组件的,因此它可以独立运行,一般是使用支持跨平台的 Kestrel 服务器(当然,在 Windows 上还可以考虑用 HttpSys,但要 ...

  3. (译文)开始学习Vue——构建你的第一个Vue应用

    我们要构建如下组件:(最终代码在这里:https://codesandbox.io/s/38k1y8x375) 开始 Vue是支持单文件组件的,但是我们不准备这么做.你也可以构建一个全局的组件,通过V ...

  4. 课后练习:C语言实现Linux命令——od

    课后练习:C语言实现Linux命令--od --------CONTENTS-------- 题目详情与分析 设计思路 遇到的问题及解决 待实现的设想与思考 学习反思与感悟 附1:myod.c「1.0 ...

  5. android 广播,manifest.xml注册,代码编写

    1.种 private void downloadBr(File file) {   // 广播出去,由广播接收器来处理下载完成的文件   Intent sendIntent = new Intent ...

  6. 项目Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  7. 【学习笔记】windows安装jhipster踏坑记录

    序: 入职新公司第二天了,本来第一天是配置环境来着,配了一下午也没搞成那个jhipster的安装,每次以为应该正常的时候都是不对,yo是yeoman的指令,但是我是使用yarn管理的yeoman 纠结 ...

  8. 机器学习中 K近邻法(knn)与k-means的区别

    简介 K近邻法(knn)是一种基本的分类与回归方法.k-means是一种简单而有效的聚类方法.虽然两者用途不同.解决的问题不同,但是在算法上有很多相似性,于是将二者放在一起,这样能够更好地对比二者的异 ...

  9. js 过多 导致页面加载过慢

    自己的代码检查了很久,才检查 出来 通常我们的网站里面会加载一些js代码,统计啊,google广告啊,百度同盟啊,阿里妈妈广告代码啊, 一堆,最后弄得页面加载速度很慢,很慢. 解决办法:换一个js包含 ...

  10. 算法题丨Remove Element

    描述 Given an array and a value, remove all instances of that value in-place and return the new length ...