当前位置:网站首页>leetCode-929: 独特的电子邮件地址
leetCode-929: 独特的电子邮件地址
2022-06-24 09:43:00 【文丑颜不良啊】
题目描述
每个有效电子邮件地址都由一个本地名和一个域名组成,以 ‘@’ 符号分隔。除小写字母之外,电子邮件地址还可以含有一个或多个 ‘.’ 或 ‘+’ 。
例如,在 [email protected]中, alice 是本地名 ,而 leetcode.com 是域名 。
如果在电子邮件地址的本地名部分中的某些字符之间添加句点(‘.’),则发往那里的邮件将会转发到本地名中没有点的同一地址。请注意,此规则不适用于域名 。
例如 “[email protected]” 和"[email protected]"会转发到同一电子邮件地址。
如果在本地名中添加加号(‘+’),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件。同样,此规则 不适用于域名 。
例如 [email protected] 将转发到 [email protected]。
可以同时使用这两个规则。
给你一个字符串数组 emails,我们会向每个 emails[i] 发送一封电子邮件。返回实际收到邮件的不同地址数目。
示例
示例 1:
输入:emails = [“[email protected]”, “[email protected]”, “[email protected]”]
输出:2
解释:实际收到邮件的是"[email protected]" 和"[email protected]"。
示例 2:
输入:emails = [“[email protected]”,“[email protected]”,“[email protected]”]
输出:3
解题过程
思路及步骤
(1)创建临时空间 Set 来保存有效的邮件地址;
(2)遍历 emails[] 数组,以 "@" 分隔每个邮件地址为两部分,一部分是本地名,一部分是域名;
(3)对于本地名中有 "+" 的,做截取操作,忽略 "+" 之后的内容;
(4)对于本地名和域名中的 "." 符号,做替换操作,将 "." 替换为 ""(空字符串)即可;
(5)将每个邮件地址放到 Set 中,由于 Set 中不允许出现重复的数据,所以最后返回 Set 中数据数量即可
代码展示
public class NumUniqueEmails {
public int numUniqueEmails(String[] emails) {
Set<String> uniqueEmailsSet = new HashSet<>();
// 以 "@" 分隔字符串为本地名和域名两部分, 只处理本地名即可
for (String email : emails) {
int j = email.lastIndexOf('@');
String localName = email.substring(0, j);
String domainName = email.substring(j);
if (localName.contains("+")) {
localName = localName.substring(0, localName.indexOf('+'));
}
localName = localName.replace(".", "");
uniqueEmailsSet.add(localName + domainName);
}
return uniqueEmailsSet.size();
}
public static void main(String[] args) {
String[] emails = {
"[email protected]", "[email protected]", "[email protected]"};
int result = new NumUniqueEmails().numUniqueEmails(emails);
System.out.println(result);
}
}
边栏推荐
- 413-二叉树基础
- Observer mode
- 静态链接库和动态链接库的区别
- NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
- Regular matching mailbox
- 正规方程、、、
- Groovy obtains Jenkins credentials through withcredentials
- PHP uses recursive and non recursive methods to create multi-level folders
- 自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏
- Producer / consumer model
猜你喜欢

队列Queue

正规方程、、、

Cookie encryption 4 RPC method determines cookie encryption

Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves

SVG+js拖拽滑块圆形进度条

Yolov6: the fast and accurate target detection framework is open source

1.项目环境搭建

机器学习——感知机及K近邻

CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算

uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
随机推荐
分布式 | 如何与 DBLE 进行“秘密通话”
Array seamless scrolling demo
物联网?快来看 Arduino 上云啦
414-二叉树的递归遍历
Wechat applet learning to achieve list rendering and conditional rendering
Error reading CSV (TSV) file
Getting user information for applet learning (getuserprofile and getUserInfo)
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
web网站开发,图片懒加载
Machine learning perceptron and k-nearest neighbor
413-二叉树基础
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
Engine localization adaptation & Reconstruction notes
NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
Network of test and development - Common Service Protocols
时尚的弹出模态登录注册窗口
美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
Internet of things? Come and see Arduino on the cloud