Wednesday, October 31, 2018

[LeetCode]Unique Email Addresses


这道题没什么好说的,按照题目的要求处理每一个email address最后看有多少unique的即可。代码如下:


class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> set;
for (auto& email : emails)
{
int idx = email.find("@");
string username = email.substr(0, idx), hostname = email.substr(idx);
string processedUsername;
for (auto& c : username)
{
if (c == '.')continue;
if (c == '+')break;
processedUsername += c;
}
string processedEmail = processedUsername + hostname;
set.insert(processedEmail);
}
return set.size();
}
};

No comments:

Post a Comment