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

Posted HERODING23

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 804 唯一摩尔斯密码词[map] HERODING的LeetCode之路相关的知识,希望对你有一定的参考价值。


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

以上是关于LeetCode 804 唯一摩尔斯密码词[map] HERODING的LeetCode之路的主要内容,如果未能解决你的问题,请参考以下文章

[804].唯一摩尔斯密码词

算法leetcode每日一练804. 唯一摩尔斯密码词

LeetCode804. 唯一摩尔斯密码词

LeetCode 804 唯一摩尔斯密码词

《LeetCode刷题》804. 唯一摩尔斯密码词(java篇)

804-唯一摩尔斯密码词