算法题目-20周-Unique Email Addresses

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

For example, in [email protected], alice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "[email protected]" and "[email protected]" forward to the same email address. (Note that this rule does not apply for domain names.)

If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example [email protected] will be forwarded to [email protected]. (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?

Example 1:

Input: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" actually receive mails

Note:

a)1 <= emails[i].length <= 100
b)1 <= emails.length <= 100
c)Each emails[i] contains exactly one '@' character.
d)All local and domain names are non-empty.
e)Local names do not start with a '+' character.

自己暴力解法,跟之前另外一个版本直接适用indexOf()和substr()速度差不多

class Solution {
    public int numUniqueEmails(String[] emails) {
          Set set = new HashSet<>();
          String address;
          boolean give ;
          boolean all ;
          for(String str:emails){
              char[] cs = str.toCharArray();
              address = "";
              give = true;
              all = false;
              for(char c :cs){
                  if(c=='+'){
                      give = false;
                  }else if(c=='@'){
                      all = true;
                  }

                  if((c!='.'&&give)||all) {
                      address = address + c;
                  }
              }
              set.add(address);
          }
          return set.size();
    }
}

Runtime: 21 ms, faster than 47.31% of Java online submissions for Unique Email Addresses.
Memory Usage: 38.2 MB, less than 98.73% of Java online submissions for Unique Email Addresses.

找一个打败99%的答案,自己忽略一个,自己直接使用String类型相加速度很慢很多

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set uniqueEmails = new HashSet<>();
        for(String email: emails){
            uniqueEmails.add(processEmail(email));
        }
        return uniqueEmails.size();
    }
    
    
    private String processEmail(String email){
        StringBuilder outEmail = new StringBuilder();
        
        boolean afterPlus = false, endEmail = false;
        for(char c: email.toCharArray()){
            if(c == '+') afterPlus = true;
            if(c == '@') endEmail = true;
            if(endEmail || (!afterPlus && c != '.')) outEmail.append(c);
        }
        
        return outEmail.toString();
    }
}

Runtime: 5 ms, faster than 99.12% of Java online submissions for Unique Email Addresses.
Memory Usage: 37.6 MB, less than 98.73% of Java online submissions for Unique Email Addresses.

自己改良之后编程以下时间(使用StringBuilder 类型)
Runtime: 9 ms, faster than 87.47% of Java online submissions for Unique Email Addresses.
Memory Usage: 38.4 MB, less than 96.20% of Java online submissions for Unique Email Addresses.

你可能感兴趣的:(算法题目-20周-Unique Email Addresses)