算法题目-20周-Unique Email Addresses

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

For example, in alice@leetcode.com, 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, "alice.z@leetcode.com" and "alicez@leetcode.com" 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 m.y+name@email.com will be forwarded to my@email.com. (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: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" 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<String> 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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,169评论 0 10
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 7,956评论 0 0
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,494评论 0 23
  • Python 全栈开发总结1.基础介绍和web.py2.Flask框架介绍3.Tornado框架介绍4.Djang...
    燕京博士阅读 7,875评论 0 3
  • 此刻,是正月初十上午九点过六分。正常的工作日,该是我上完早读,吃过早点,上完第一节课,喝口水或整理教案,准备第...
    魏文晶阅读 1,427评论 0 2

友情链接更多精彩内容