这道题没什么好说的,按照题目的要求处理每一个email address最后看有多少unique的即可。代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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