Java/929. Unique Email Address 独特的电子邮件地址

90 篇文章 0 订阅
1 篇文章 0 订阅

题目


 

 

 

代码部分一(96ms 26.74%)

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        int res = 0;
        
        String[] str = new String[2];
        int len = emails.length;
        for(int i = 0; i < len; i++){
            str = emails[i].split("@");
            String temp = str[0].replaceAll("\\.", "");
            int end = temp.indexOf("+");
            if(end != -1) str[0] = temp.substring(0, end);
            if(!set.contains(str[0]+ "@" +str[1])){
                res++;
                set.add(str[0] + "@" + str[1]);
            }
        }
        
        return res;
    }
}

 

代码部分二(75ms 47.21%)

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        int res = 0;
        
        String[] str = new String[2];
        int len = emails.length;
        for(int i = 0; i < len; i++){
            str = emails[i].split("@");
            String temp = "";
            int end = str[0].indexOf("+");
            if(end != -1) temp = str[0].substring(0, end);
            else temp = str[0];
            str[0] = "";
            char[] ch = temp.toCharArray();
            for(int j = 0; j < ch.length; j++){
                if(ch[j] != '.') str[0] += ch[j];
            }
            if(!set.contains(str[0]+ "@" +str[1])){
                res++;
                set.add(str[0] + "@" + str[1]);
            }
        }
        
        return res;
    }
}

 

 

代码部分三(27ms 85.27%)

class Solution {
    public int numUniqueEmails(String[] emails) {
         Set<String> set = new HashSet<>();
	       String last = "";
	       String first = "";
	       for(String email : emails) {
	    	   last = email.substring(email.indexOf("@"));
	    	   first = email.substring(0,email.indexOf("@"));
	    	   char [] arr = new char[first.length()];
	    	   arr = first.toCharArray();
	    	   int j = 0;
	    	   char [] arr1 = new char[100];
	    	   for(int i = 0; i < arr.length; i++) {
	    		   if(arr[i] == '.') {
	    			   continue;
	    		   }
	    		   if(arr[i] == '+') {
	    			   break;
	    		   }
	    		   arr1[j] = arr[i];
	    		   j++;
	    	   }
	    	   set.add(first.valueOf(arr1).trim()+last);
	       }
	       return set.size();
    }
}

 

 

代码部分四(9ms 100%)

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for(String str : emails){
            int n = str.indexOf('@');
            String temp = str.substring(n);
            set.add(temp);
        }
        
        return set.size();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值