LeetCode 929 Unique Email Addresses

栏目: 编程工具 · 发布时间: 7年前

内容简介:给与一组其中按照题意,先取

给与一组 email 地址, email 分为两部分, @ 前我们称为 local name , @ 后称为 domain name 。现规定只可以出现小写字母和字符 .+

其中 . 可以忽略, 如 `alice.z@leetcode.com al.icez@leetcode.com 都表示 alicez@leetcode.com + 出现在 local name 中时,将忽略第一个 + 后的所有内容,如 m.y+name@email.com m.y@email.com 都表示 my@email.com `。

解法

按照题意,先取 local name 的第一个字符到第一个加号之前的内容, 然后删除其中的所有 . , 并与 @ 后的 domain name 拼接, 将结果放入 Set 集合中,最后返回 Set 的长度即可。

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();

        for (String email : emails) {
            int start = email.indexOf("+");
            int end = email.indexOf("@");
            String localName = email.substring(0, start);
            localName = localName.replace(".", "");
            String domainName = email.substring(end, email.length());
            set.add(localName + domainName);
        }
        return set.size();
    }
}
Runtime: 35 ms, faster than 63.93% of Java online submissions for Unique Email Addresses.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google 广告高阶优化(第3版)

Google 广告高阶优化(第3版)

【美】Brad Geddes(布兰德.盖兹) / 宫鑫、康宁、王娜 / 电子工业出版社 / 2015-9 / 99.00元

《Google 广告高阶优化(第3版)》可以说是Google AdWords的终极指南,内容非常丰富,第三版在内容上进行了全面更新。介绍了AdWords的最新最完整的功能,阐释其工作原理,也提供了相应的优化方法、策略和实践教程,读者可以随时在自己的PPC广告系列中进行实践。第三版增添了50多页新内容,涵盖Google系统最近的所有变动,包括广告系列结构的变化、出价调整器、重定向、视频广告功能、全新......一起来看看 《Google 广告高阶优化(第3版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具