LeetCode 804 唯一摩尔斯密码词[map] HERODING的LeetCode之路

LeetCode 804 唯一摩尔斯密码词[map] HERODING的LeetCode之路,第1张


解题思路:
一道非常简单的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;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/577969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存