1,题目要求
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.
也就是说,每个字母都对应一个摩尔斯码。给定单词,将其转化为摩尔斯电码,然后看一共有几种不同的摩尔斯码。
2,题目思路
这是一道比较新的LeetCode的题目,同时也比较简单。整体思路就是直接转换,然后利用set(集合)容器的特性->自动排除重复元素,获得最后的答案。
3,程序源码
static vector<string> MORSE = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
unordered_set<string> resSet;
for(auto word : words)
{
string tmp = "";
for(auto alp : word)
tmp += MORSE[alp-'a'];
resSet.insert(tmp);
}
return resSet.size();
}
};
在C++的类定义内,static成员前的static意思是对一个变量的声明,而非定义。静态成员变量的初始化通常必须在类的定义外,即.cpp文件里而不是.h里。
也就是说,static不可以写在Solution内。