LeetCode题解之Unique Morse Code Words

1、题目描述

2、题目分析

将words 中的每一个string  直接翻译成对应的Morse 码,然后将其放入 set 中,最后返回set的大小即可,此处利用的set 中元素不重复的性质。

3.代码

 1 vector<string> Morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",
 2                                 ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
 3                                 "...","-","..-","...-",".--","-..-","-.--","--.."};
 4         std::set<std::string> myset;
 5         
 6         for( auto &s: words)
 7         {
 8             string morse_s;
 9             for( auto it = s.begin() ; it !=s.end() ; ++it)
10             {
11                 morse_s += Morse[ *it - 'a' ];
12             }
13             myset.insert( morse_s );
14         }
15         return myset.size();
16         
17     }

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/9294643.html