当前位置:网站首页>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);
}
}
边栏推荐
- H5网页如何在微信中自定义分享链接
- 415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
- YOLOv6:又快又准的目标检测框架开源啦
- canvas管道动画js特效
- [db2] sql0805n solution and thinking
- SQL Server AVG function rounding
- 学习整理在php中使用KindEditor富文本编辑器
- 411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
- SSH Remote Password free login
- What are the characteristics of EDI local deployment and cloud hosting solutions?
猜你喜欢

Basic operations on binary tree

SQL Server AVG函数取整问题

Use of vim

NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!

How large and medium-sized enterprises build their own monitoring system

解决Deprecated: Methods with the same name as their class will not be constructors in报错方案

p5.js千纸鹤动画背景js特效

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

How to manage massive network infrastructure?

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
随机推荐
Using pandas to read SQL server data table
414-二叉树的递归遍历
被困英西中学的师生安全和食物有保障
Nvisual digital infrastructure operation management software platform
El table Click to add row style
线程池的执行流程
操作符详解
队列Queue
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
oracle池式连接请求超时问题排查步骤
学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
413-二叉树基础
Phpstrom code formatting settings
[db2] sql0805n solution and thinking
indexedDB本地存储,首页优化
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
SQL Sever中的窗口函数row_number()rank()dense_rank()
tf.errors
H5网页如何在微信中自定义分享链接
The great charm of cookies