当前位置:网站首页>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网页如何在微信中自定义分享链接
- Cookie encryption 4 RPC method determines cookie encryption
- Three ways to use applicationcontextinitializer
- Programming questions (continuously updated)
- 记录一下MySql update会锁定哪些范围的数据
- Network of test and development - Common Service Protocols
- 411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
- 414-二叉树的递归遍历
- SVG+js拖拽滑块圆形进度条
- 涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
猜你喜欢
Wechat applet learning to achieve list rendering and conditional rendering
Using pandas to read SQL server data table
Three ways to use applicationcontextinitializer
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Arbre binaire partie 1
上升的气泡canvas破碎动画js特效
Internet of things? Come and see Arduino on the cloud
操作符详解
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
2021-08-17
随机推荐
GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
Machine learning perceptron and k-nearest neighbor
Which of the top ten securities companies has the lowest Commission and is the safest and most reliable? Do you know anything
leetCode-498: 对角线遍历
学习整理在php中使用KindEditor富文本编辑器
Programming questions (continuously updated)
numpy.logical_and()
SQL sever试题求最晚入职日期
leetCode-面试题 01.05: 一次编辑
学习使用php对字符串中的特殊符号进行过滤的方法
Three ways to use applicationcontextinitializer
读取csv(tsv)文件出错
How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
2.登陆退出功能开发
Floating point notation (summarized from cs61c and CMU CSAPP)
Use of vim
Impdp leading schema message ora-31625 exception handling
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
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