这是悦乐书的第356次更新,第383篇原创

01看题和准备

今天介绍的是LeetCode算法题中Easy级别的第218题(顺位题号是927)。每封电子邮件都包含本地名称和域名,以@符号分隔。

例如,在alice@leetcode.com中,alice是本地名称,leetcode.com是域名。

除了小写字母,这些电子邮件可能包含'.''+'

如果在电子邮件地址的本地名称部分中的某些字符之间添加句点('.'),则在那里发送的邮件将转发到本地名称中没有点的同一地址。例如,“alice.z@leetcode.com”“alicez@leetcode.com”转发到同一个电子邮件地址。(请注意,此规则不适用于域名。)

如果在本地名称中添加加号('+'),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件,例如m.y+name@email.com将转发到my@email.com。(同样,此规则不适用于域名。)

可以同时使用这两个规则。

给定电子邮件列表,我们会向列表中的每个地址发送一封电子邮件。有多少不同的地址实际接收邮件?例如:

输入:[“test.email+alex@leetcode.com”,“test.e.mail+bob.cathy@leetcode.com”,
“testemail+david@lee.tcode.com”]
输出:2
说明:“testemail@leetcode.com”和“testemail@lee.tcode.com”实际收到邮件

注意

  • 1 <= emails[i].length <= 100

  • 1 <= emails.length <= 100

  • 每封emails[i]都包含一个'@'字符。

  • 所有本地名称和域名都是非空的。

  • 本地名称不以“+”字符开头。

02 第一种解法

根据题目给的规则,对字符串分两段处理,在本地名称中,如果存在+号,就截取+号前的字符串,变成新的本地名称,如果遇到点号,就将点号全部替换,得到新的本地名称,再和域名部分拼接在一起,存入HashSet中,返回HashSetsize即可。

public int numUniqueEmails(String[] emails) {
Set<String> set = new HashSet<String>();
for (String email : emails) {
String tem = email;
int index = tem.indexOf('@');
// 截取@之前的字符串
tem = tem.substring(0, index);
// 有'+',就再截取'+'号之前的字符串
if (tem.indexOf('+') > 0) {
tem = tem.substring(0, tem.indexOf('+'));
}
// 将所有点号替换
tem = tem.replace(".", "");
// 将@符号前的新字符串和原字符串@符号后的域名拼接
// 存入HashSet
set.add(tem+email.substring(index));
}
return set.size();
}

03 第二种解法

我们也可以不使用字符串截取、替换等方法,直接对字符进行判断,然后拼接成新的email地址,最后存入HashSet中,返回HashSetsize即可。

public int numUniqueEmails2(String[] emails) {
Set<String> set = new HashSet<String>();
for (String email : emails) {
set.add(handleEmail(email));
}
return set.size();
} public String handleEmail(String email) {
StringBuilder sb = new StringBuilder();
int n = email.length();
// 是否遇到@符号
boolean flag = false;
for (int i=0; i<n; i++) {
char c = email.charAt(i);
if (!flag) {
//没遇到@符号前,遇到.号一律 跳过
if (c == '.') {
continue;
}
//遇到+号,往后跳一位,开始寻找@符号
if (c == '+') {
i++;
while (i < n && email.charAt(i) != '@') {
i++;
}
c = email.charAt(i);
// 已经遇到@符号
flag = true;
} else if(c == '@'){
// 没遇到+号,却遇到@符号了
flag = true;
}
}
sb.append(c);
}
return sb.toString();
}

04 小结

算法专题目前已连续日更超过六个月,算法题文章224+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.927-独特邮箱地址(Unique Email Addresses)的更多相关文章

  1. [Swift]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

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

  2. [leetcode] 929. Unique Email Addresses (easy)

    统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...

  3. 929. Unique Email Addresses

    929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...

  4. 【Leetcode_easy】929. Unique Email Addresses

    problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...

  5. [LeetCode] 929. Unique Email Addresses 独特的邮件地址

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

  6. [LeetCode] 929. Unique Email Addresses 唯一的电邮地址

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

  7. 【LeetCode】929. Unique Email Addresses 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set + 字符串操作 参考资料 日期 题目地址:h ...

  8. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

  9. LeetCode 929.Unique Email Addresses

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

随机推荐

  1. springmvc其他类获取request记得web.xml

    <listener> <listener-class>org.springframework.web.context.request.RequestContextListene ...

  2. UVALive - 3510 Pixel Shuffle (置换)

    题目链接 有一个n*n的图像和7种置换,以及一个置换序列,求将这个序列重复做几次能得到原图像. 将这些置换序列乘起来可得到一个最终置换,这个置换所有循环节的长度的lcm即为答案. 注意置换是从右往左进 ...

  3. 【每日一包0008】arr-diff

    [github地址:https://github.com/ABCDdouyae...] arr-diff 多个数组比较,过滤出第一个数组独有的内容 用法:arr-diff(arr1, arr2, ar ...

  4. Kendo UI for jQuery使用教程:使用MVVM初始化(二)

    [Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...

  5. 关于 Vue 微信客户端 不能播放音乐(报错和不能播放的问题)

    前言 用vue 做音乐播放的时候,在本地可以打开播放,但在微信里面不能播放音乐 所以这样解决 // 音乐播放 audioPlay(){ let _this = this; var audio = _t ...

  6. c++拓展register寄存器

    寄存器----硬件和软件打交道的接口,这上面装了android,亦或是 windows,linux就能和相关的操作系统设备打交道 早期硬件性能并不很快时,为了提高程序运行的效率,会声明一个寄存器变量, ...

  7. chr ord 去重

    找不同字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. def func(s, t): num1 = 0 num2 = 0 for i in s: nu ...

  8. ng-class的几种用法

    参考来自 https://www.cnblogs.com/zhoulin1234/p/9587955.html 方法1.逻辑在后面的中括号里面 ng-class="{true : 'chec ...

  9. Linux 系统中 grep 的ABC参数含义

    1.grep  -A  5   匹配行及后5行 2.grep  -B  5   匹配行及前5行 3.grep  -C  5   匹配行及前后各5行

  10. OpenCV笔记(2)(高斯平滑、腐蚀和膨胀、开闭运算、礼帽和黑帽、Sobel及其他算子)

    一.高斯平滑(模糊) def gaussian_blur(image): # 设置ksize来确定模糊效果 img = cv.GaussianBlur(image, (5, 5), 0) cv.ims ...