题目链接:804. Unique Morse Code Words
解题思路:没什么技巧,构造 map 作为密码字典,生成的摩斯密码插入到一个 unordered_set 中,因为元素具有唯一性,所以可以直接求个数。
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
std::map<char, string> morse{
{'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', "--.."}};
unordered_set<string> hash;
for (string &s:words)
{
string t ("");
for (char &c:s) t += morse[c];
hash.insert(t);
}
return hash.size();
}
};
本文介绍了一种使用C++解决LeetCode题目804.Unique Morse Code Words的方法。通过构造摩尔斯电码字典并利用unordered_set的唯一性特点,实现对不同单词转换为摩尔斯电码后的数量统计。
&spm=1001.2101.3001.5002&articleId=88194100&d=1&t=3&u=0cae500813494156872784a9426c2a4d)
3990

被折叠的 条评论
为什么被折叠?



