Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.comalice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.  (Note that this rule does not apply for domain names.)

If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com.  (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

Note:

  • 1 <= emails[i].length <= 100
  • 1 <= emails.length <= 100
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.

博主正在刷题的时候,突然朋友圈刷出了科比坠机的消息,惊的下巴都掉了,忙看了下日期,不是四月一啊,于是疯狂的 google,中文搜不到任何相关的消息,于是搜英文 Kobe Bryant,结果真的有坠机消息,而且是几分钟前刚发布的,渐渐的很多微信群里都开始讨论了,连 wiki 上都更新了,随着越来越多的媒体确认这一个消息,心情越来越沉重了。算起来了,在博主最早关注 NBA 的时候,科比就当红球星,二十年的光辉岁月,五座总冠军戒指,甚至退役后还获得过奥斯卡小金人,年仅四十一岁,本来是要续写另一段传奇人生,就这么的走了?人生无常啊,你永远不知道意外和明天哪一个先到来,能平平安安的活着就已经是万幸了。RIP,一路走好,科比,愿天堂没有直升机。下面带着沉重的心情来做题吧,这道题是关于邮件的,邮件名里可能会有两个特殊符号,点和加号,对于点采取直接忽略的做法,对于加号则是忽略其后面所有的东西,现在问我们有多少个不同的邮箱。没有太多的技巧,就直接遍历一下所有的字符,遇到点直接跳过,遇到 '+' 或者 '@' 直接 break 掉。注意这里其实有个坑,就是域名中也可能有点,而这个点是不能忽略的,所以要把 '@' 及其后面的域名都提取出来,连到之前处理好的账号后面,一起放到一个 HashSet 中,利用其可以去重复的特性,最终剩余的个数即为所求,参见代码如下:

class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> st;
for (string email : emails) {
string name;
for (char c : email) {
if (c == '.') continue;
if (c == '+' || c == '@') break;
name.push_back(c);
}
name += email.substr(email.find('@'));
st.insert(name);
}
return st.size();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/929

参考资料:

https://leetcode.com/problems/unique-email-addresses/

https://leetcode.com/problems/unique-email-addresses/discuss/317207/C%2B%2B-Concise-Solution

https://leetcode.com/problems/unique-email-addresses/discuss/186798/Java-7-liner-with-comment.

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

最新文章

  1. Eclipse 各版本版本号代号对应一览表
  2. Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)
  3. Appium UI自动化的那些梗
  4. JavaWeb---总结(十四)JSP原理
  5. C语言-结构体
  6. response的outputStream输出数据的问题
  7. ehcache的介绍和使用
  8. Session变量不能传送到下一页.解决: session.use_trans_sid = 1
  9. Python try/except异常处理机制
  10. Delegate&amp;Event
  11. STL map+vector+struct的使用示例
  12. C# 定时任务
  13. asp.net 根据连接地址保存文件,图片
  14. vs相同变量高亮显示
  15. tcp网络通信的三次握手与三次挥手
  16. iconv用法解读
  17. 揭秘DOM中data和nodeValue属性同步改变那些事
  18. poj 1113 Wall 凸包的应用
  19. C++空类和string类
  20. 64位MATLAB和C混合编程以及联合调试

热门文章

  1. 「JOI2019 Final」解题报告
  2. JS垂直落体回弹原理
  3. 2020年digitalocean最新优惠码100美元奖励
  4. 1. 使用 Docker 安装 Kong
  5. js通过cookie对两个没有关系的jsp页面进行传值
  6. day10-Python运维开发基础(函数嵌套、nonlocal声明局部变量、闭包、locals/globals、lambda表达式)
  7. Android反编译与防止反编译
  8. 「SP1716」GSS3 - Can you answer these queries III
  9. Docker 安装(centos7下)
  10. 基于PIL模块创建验证码图片