0
点赞
收藏
分享

微信扫一扫

2022/04/010 leetcode 每日一题 804. 唯一摩尔斯密码词(简单题)

古得曼_63b6 2022-04-13 阅读 54
leetcode

1.题目

2.求解

今天是一道简单题,看见就大概知道怎么求的题目,就是不知道会不会有更好的办法。

        讲一下我的方法,遍历每一个字母,去定义的数组内查找每个字母对应的字符串,拼接存到map里面就好了。等所有的words遍历完,查找map内有多少元素即可,很简单。

3.代码

 

class Solution {
    string nums[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        map<string, int> wordsmap;
        int n = words.size();
        int i, j, k;
        for(i = 0; i < n;i ++){
            string cnt = "";
            for(j = 0; j < words[i].size(); j++){
                int idx = words[i][j] - 'a';
                cnt += nums[idx];
            }
            if(!wordsmap.count(cnt)){
                wordsmap[cnt] = 1;
            }
        }
        return wordsmap.size();
    }
};
举报

相关推荐

0 条评论