929. Unique Email Address - LeetCode
Question

Solution
题目大意:
给你一个邮箱地址的数组,求出有多少个不同的地址,其中localName有如下规则
- 加号(+)后面的字符及加号忽略
- 点(.)也忽略
思路:
直接遍历,字符串操作即可
Java实现:
public int numUniqueEmails(String[] emails) {
if (emails == null || emails.length == 0) {
return 0;
}
Set<String> localNames = new HashSet<>();
for (String email : emails) {
String[] arr = email.split("@");
String localName = arr[0];
// 忽略加号(+)后边的字符
int indexOfPlus = localName.indexOf("+");
if (indexOfPlus != -1) {
localName = localName.substring(0, indexOfPlus);
}
// 替换逗号
localName = localName.replaceAll("\\.", "");
localNames.add(localName + "@" + arr[1]);
}
return localNames.size();
}
在处理地址的时候,用下面的方法取localName和domainName更快
int indexOfAt = email.indexOf("@");
String localName = email.substring(0, indexOfAt);
String domainName = email.substring(indexOfAt);
929. Unique Email Address - LeetCode的更多相关文章
- 929. Unique Email Addresses
929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...
- 【Leetcode_easy】929. Unique Email Addresses
problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...
- [LeetCode] 929. Unique Email Addresses 唯一的电邮地址
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- [LeetCode] 929. Unique Email Addresses 独特的邮件地址
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- LeetCode 929.Unique Email Addresses
Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...
- LeetCode 929 Unique Email Addresses 解题报告
题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...
- 【leetcode】929. Unique Email Addresses
题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...
- 【LeetCode】929. Unique Email Addresses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set + 字符串操作 参考资料 日期 题目地址:h ...
- [leetcode] 929. Unique Email Addresses (easy)
统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...
随机推荐
- SQL数据库之IFNULL函数和NULLIF函数
学习IFNULL()函数 非空判断 解析 IFNULL(expression1, expression2) 如果expression1为null, 在函数返回expression2,否则将返回expr ...
- HMS Core 分析服务 6.4.1版本上线啦,快来看看更新了哪些内容。
更新概览 支持转化事件回传至华为应用市场商业推广,便捷归因,实时调优. 卸载分析模型支持用户卸载前事件和路径分析,深度剖析卸载根因. 实时漏斗体验开放,灵活定位异常流失. 详情介绍 更新一:全面开放深 ...
- validator API文档
如何使用 引入 <script src="../node_modules/jquery/dist/jquery.js"></script> <scri ...
- 基于vue-cli搭了一个多页面应用的空脚手架
vue2.* (多页面跳转) @[vue2.3.3|webpack2.6.1|less|axios] 之前看过有相关朋友share了空的多页面脚手架. 不过down了几个都是webpack1.0或者v ...
- JS 中的日期时间操作计算实例
实例 一:已知日期格式为 "YYYY/MM/DD",计算相对于今天的天数差. function fromNow(date){ var mTimes = new Date(date) ...
- python-排列组合序列
[题目描述]用户输入整数n(1<=n<=26)和整数m(m<=n),然后输入n个不同的字母,请编写程序输出在这n个字母中选择m个字母的所有排列序列和组合序列. [练习要求]请给出源代 ...
- openlayers离线瓦片地图开发
近期业务繁忙...待更新
- python---二叉树广度优先和深度优先遍历的实现
class Node(object): """结点""" def __init__(self, data): self.data = dat ...
- Spring-aop注解开发(切点表达式的抽取)
接上一篇aop注解快速开发 @Component @Aspect //标注当前aspect是切面类 public class MyAspect { @Before("Pointcut()&q ...
- 数组-LeetCode-笔试
目录 数组理论基础 二分查找 二分法第一种写法 二分法第二种写法 ACM 移除元素 暴力解法 双指针法(快慢指针) ACM 有序数组的平方 暴力排序 双指针法 长度最小的子数组 暴力解法 滑动窗口 相 ...