Leetcode 804 Unique Morse Code Words 莫尔斯电码重复问题
2018-03-25 11:05
549 查看
题目描述:International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
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
Each
翻译:摩尔斯电码是使用.-来表示字母,如果直接将字符串转换成摩尔斯电码,而没有空格的话,那么不同的字符串可能有相同的摩尔斯电码,下面给出一系列字符串,给出这些字符串能够得到多少种不同类的摩尔斯电码。
思路:
1.首先根据字母拼接对应的摩尔斯电码,然后将这一段电码做hash映射,可以看做是一段01串,直接转化成二进制即可(可能溢出int范围,但是没有关系,相同的字符串溢出后也是一样的)
2.将一系列字符串对应的hash值排序,去重即可。
代码:class Solution {
public:
vector<string> mos={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
string to_mos(string &word)
{
string ans="";
for(int i=0;i<word.size();i++)
ans += mos[word[i]-'a'];
return ans;
}
int removeDuplicates(vector<int>& 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<m.size();i++)
{
if(m[i]=='-') ans++;
ans*=2;
}
return ans;
}
int uniqueMorseRepresentations(vector<string>& words)
{
int n=words.size();
vector<string> mos_words(n);
for(int i=0;i<n;i++)
mos_words[i] = to_mos(words[i]);
vector<int> mos_words_hash(n,0);
for(int i=0;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);
}
};
"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
wordswill 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=0;i<word.size();i++)
ans += mos[word[i]-'a'];
return ans;
}
int removeDuplicates(vector<int>& 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<m.size();i++)
{
if(m[i]=='-') ans++;
ans*=2;
}
return ans;
}
int uniqueMorseRepresentations(vector<string>& words)
{
int n=words.size();
vector<string> mos_words(n);
for(int i=0;i<n;i++)
mos_words[i] = to_mos(words[i]);
vector<int> mos_words_hash(n,0);
for(int i=0;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
- 804. Unique Morse Code Words
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
- 357.leetcode Count Numbers with Unique Digits(easy)[数学问题 非重复数字]
- LeetCode Count Numbers with Unique Digits(计数问题)
- leetcode 日经贴,python code -Reverse Words in a String
- 排颜色问题——数组 leetcode lintcode
- Powerdesigner 设计主键code不能重复等问题
- LeetCode(一)之java集合重复元素问题
- [LeetCode系列]子集枚举问题[有重复元素]
- leetcode 47. Permutations II 全排列问题(去掉重复元素)+递归
- [LeetCode] First Unique Character in a String 寻找第一个不重复出现的字符
- DP问题:leetcode(62) Unique Paths
- 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
- UVA 11572 Unique Snowflakes(最长无重复元素问题) 用dp思路
- LintCode Unique Characters 判断字符串是否没有重复字符
- Leetcode中的回溯法题目总结:八皇后问题; unique path问题;subsets问题
- DP问题:leetcode(96) Unique Binary Search Trees
- C++ 写leetcode遇到的一些问题总结Reverse Words in a String
- [leetcode 96]Unique Binary Search Trees--二叉搜索树的个数问题。