力扣算法——独特的电子邮件地址

一、代码实现

每个 有效电子邮件地址 都由一个 本地名 和一个 域名 组成,以 ‘@’ 符号分隔。除小写字母之外,电子邮件地址还可以含有一个或多个 ‘.’ 或 ‘+’ 。

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

例如,"alice.z@leetcode.com” 和 “alicez@leetcode.com” 会转发到同一电子邮件地址。
如果在 本地名 中添加加号(‘+’),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件。同样,此规则 不适用于域名 。

例如 m.y+name@email.com 将转发到 my@email.com。
可以同时使用这两个规则。

给你一个字符串数组 emails,我们会向每个 emails[i] 发送一封电子邮件。返回实际收到邮件的不同地址数目。

基本本地名需要修改,所以用@分层,有两种修改规则。
1.有点的去掉
2.到加号截至

//删除法
class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> emailSet = new HashSet<String>();
        for (String email : emails) {
            int i = email.indexOf('@');
            String local = email.substring(0, i).split("\\+")[0]; // 去掉本地名第一个加号之后的部分
            local = local.replace(".", ""); // 去掉本地名中所有的句点
            emailSet.add(local + email.substring(i));
        }
        return emailSet.size();
    }
}
//拼接法
class Solution {
 public int numUniqueEmails(String[] emails) {
        Set<String> records = new HashSet<>();
        for(int i = 0; i < emails.length; i++){
            String[] part = emails[i].split("@");
            StringBuffer sb = new StringBuffer();
            for(int j = 0; j < part[0].length(); j++){
                if(part[0].charAt(j) == '.'){
                    continue;
                }
                if(part[0].charAt(j) == '+'){
                    break;
                }
                sb.append(part[0].charAt(j));
            }
            records.add(sb.append("@").append(part[1]).toString());
        }
        return records.size();
    }
}

二、删除法知识点

知识点来自菜鸟教程

//public String substring(int beginIndex)
//public String substring(int beginIndex, int endIndex)
//beginIndex -- 起始索引(包括), 索引从 0 开始。
//endIndex -- 结束索引(不包括)
public class RunoobTest {
    public static void main(String args[]) {
        String Str = new String("This is text");
 //返回值:is text
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 //返回值:is te
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );
    }
}

//public String[] split(String regex, int limit)
//regex 分隔符 limit 分割份数
public class Test {
    public static void main(String args[]) {
        String str = new String("Welcome-to-Runoob");
 //Welcome  to  Runoob
        System.out.println("- 分隔符返回值 :" );
        for (String retval: str.split("-")){
            System.out.println(retval);
        }
 //Welcome to-Runoob
        System.out.println("");
        System.out.println("- 分隔符设置分割份数返回值 :" );
        for (String retval: str.split("-", 2)){
            System.out.println(retval);
        }
 //www runoob com
        System.out.println("");
        String str2 = new String("www.runoob.com");
        System.out.println("转义字符返回值 :" );
        for (String retval: str2.split("\\.", 3)){
            System.out.println(retval);
        }
 //acount=?  uu =?  n=?
        System.out.println("");
        String str3 = new String("acount=? and uu =? or n=?");
        System.out.println("多个分隔符返回值 :" );
        for (String retval: str3.split("and|or")){
            System.out.println(retval);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值