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 "-..."
, "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 most100
. - 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 莫尔斯电码重复问题的更多相关文章
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)
题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode 804 Unique Morse Code Words 解题报告
题目要求 International Morse Code defines a standard encoding where each letter is mapped to a series of ...
- [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 ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
随机推荐
- 介绍C语言指针
最近心态不太好,但是还是控制自己刷一下算法题,但是看着多次出现的 “Segmentation fault”,心态又爆炸啦.我只想说:“我也早觉得有写一点东西的必要了.离三月十八日也已有两星期,忘却的救 ...
- Leetcode 15——3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- C语言二维数组作业
一.PTA实验作业 题目1:7-3 出生年 1. 本题PTA提交列表 2. 设计思路 1.声明一个函数different()用来计算一个年份的不同数字个数 2.定义y(y是来计算符合要求的年份的量), ...
- 软件工程结对编程-2017282110264&2017282110249
0 小组成员 李世钰 / 2017202110264 王成科 / 2017282110249 1 项目 GitHub 地址 && 演示地址 GitHub: https://github ...
- 20162311张之睿 Linux基础与Java开发环境实验报告
实验一 Java开发环境的熟悉 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习& ...
- Beta冲刺第四天
一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:修复协作详情,日程详情日程类型显示纠正 2.黄腾达:修复管理者查看协作成员可以移除自己的问题,加入登录.注册表单按回车键就可直接完成操作的功能 3 ...
- Bate版敏捷冲刺报告--day0
1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285) Git链接:https://github.com/WHUSE2017/C-team 2 ...
- selenium webdriver API
元素定位 #coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary imp ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...