当前位置:网站首页>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);
}
}
边栏推荐
- canvas管道动画js特效
- 引擎国产化适配&重构笔记
- Error reading CSV (TSV) file
- Groovy obtains Jenkins credentials through withcredentials
- 413-二叉树基础
- 机器学习——主成分分析(PCA)
- SSH Remote Password free login
- numpy.linspace()
- Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
- 学习使用php实现无限极评论和无限极转二级评论解决方案
猜你喜欢

3.员工的增删改查

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

Phpstrom code formatting settings

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

canvas掉落的小球重力js特效动画

SQL Sever关于like操作符(包括字段数据自动填充空格问题)

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

Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.

微信小程序學習之 實現列錶渲染和條件渲染.

Wechat applet learning to achieve list rendering and conditional rendering
随机推荐
np.float32()
PostgreSQL DBA quick start - source compilation and installation
Producer / consumer model
Geogebra instance clock
p5.js实现的炫酷交互式动画js特效
Floating point notation (summarized from cs61c and CMU CSAPP)
Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
MYSQL数据高级
整理接口性能优化技巧,干掉慢代码
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
port 22: Connection refused
El table Click to add row style
canvas无限扫描js特效代码
Three ways to use applicationcontextinitializer
Error reading CSV (TSV) file
SQL sever试题求最晚入职日期
Symbol. Iterator iterator
413-二叉树基础
机器学习——主成分分析(PCA)