天天看点

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

题目

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

代码部分一(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();
    }
}