Question

929. Unique Email Address

Solution

题目大意:

给你一个邮箱地址的数组,求出有多少个不同的地址,其中localName有如下规则

  1. 加号(+)后面的字符及加号忽略
  2. 点(.)也忽略

思路:

直接遍历,字符串操作即可

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的更多相关文章

  1. 929. Unique Email Addresses

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

  2. 【Leetcode_easy】929. Unique Email Addresses

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

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

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

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

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

  5. LeetCode 929.Unique Email Addresses

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

  6. LeetCode 929 Unique Email Addresses 解题报告

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

  7. 【leetcode】929. Unique Email Addresses

    题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...

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

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

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

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

随机推荐

  1. 8_根轨迹_Part2_根轨迹手绘技巧

    传递函数分母部分相同

  2. 一个让我很不爽的外包项目——奔驰Smart2015新官网

    七月份的下半个月,有幸做了奔驰 Smart 2015年新官网,包括手机端和PC端的宣传页,地址: PC端 手机端 这里,为了证明这个是一个事实,我还特意的留存了两张截图: 这里只想说明这么几个问题: ...

  3. ecahrts实现动态刷新(隔几秒重新显示)

    代码: <script> var chartDom = document.getElementById('main3'); var myChart = echarts.init(chart ...

  4. python-杨辉三角形

    [题目描述]输出n(0<n)行杨辉三角形,n由用户输入. [练习要求]请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释. [输入格式]一行中输入1个整数n. [输出格式]输出n行杨辉 ...

  5. Hadoop 3.1.2报错:xception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"

    报错内容如下: Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No ...

  6. 浅谈ES6中的Async函数

    转载地址:https://www.cnblogs.com/sghy/p/7987640.html 定义:Async函数是一个异步操作函数,本质上,Async函数是Generator函数的语法糖.asy ...

  7. [ SOS ] 版本控制工具 笔记

    https://www.cnblogs.com/yeungchie/ soscmd 创建工作区 soscmd newworkarea $serverName $projectName [$path] ...

  8. Java对象和多态

    Java对象和多态 (面向对象) 面向对象基础 面向对象程序设计(Object Oriented Programming) 对象基于类创建,类相当于一个模板,对象就是根据模板创建出来的实体(就像做月饼 ...

  9. MySQL 表数据多久刷一次盘?

    前言 事情是这样的,在某乎的邀请回答中看到了这个问题: - 然后当时我没多想就啪一下写下来这样的答案: 这个其实要通过 MySQL 后台线程来刷的,在 Buffer Pool 中被修改的过的 Page ...

  10. shell、bash和sh区别

    shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核. shell 是一个命令语言解释器(co ...