804. 唯一摩尔斯密码词 leetcode
class Solution {
static array<const string , 26> ops;
public:
int uniqueMorseRepresentations(vector<string>& words) {
unordered_set<string> pool;
for(auto && s:words){
string tmp = {
};
for(char c : s)
{
tmp.insert(tmp.size(), ops[c-a]);
}
pool.emplace(move(tmp));
}
return pool.size();
}
};
array<const string,26> Solution::ops={
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
常规做法
class Solution {
private:
vector<string> encode = {
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
public:
int uniqueMorseRepresentations(vector<string>& words) {
if(words.size() == 0) return 0;
// set 存储
unordered_set<string> st;
// stringstream处理字符串
stringstream ss;
for(const auto& str:words){
// 遍历单词
ss.str(""); // 清空ss
for(const auto& c:str){
// 单词转换
ss << encode[c-a];
}
st.emplace(ss.str()); // 加入集合
}
return st.size(); // 返回集合大小
}
};
上一篇:
IDEA上Java项目控制台中文乱码
