解题思路:
一道非常简单的key value映射题目。
将对应的摩尔斯密码存储在数组中,然后用map记录组成的单词,如果有重复就直接跳过,没有就ans++,对应的key也++,代码如下:
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
unordered_map<string, int> mp;
string letters[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int ans = 0;
for(int i = 0; i < words.size(); i ++) {
string temp;
for(auto& letter : words[i]) {
temp += letters[letter - 'a'];
}
if(mp.count(temp)) {
continue;
} else {
mp[temp] ++;
ans ++;
}
}
return ans;
}
};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)