zoukankan      html  css  js  c++  java
  • [LeetCode] 929. Unique Email Addresses

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

    For example, in alice@leetcode.comalice 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:

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

    独特的电子邮件地址。题意是给一个input数组,里面是一些email地址的字符串,请根据规则判断到底有几个独特的email地址。规则如下

    • 遇到点就忽略
    • 遇到加号就忽略加号后面的部分

    既然是判断独特的东西,那么一定会用到hashset。首先遍历input,将每一个字符串里面@的index找到,然后将字符串分成name,@和domain三个部分。之后接着处理name的部分,就需要按照如上两个规则,遇到点就跳过continue,遇到加号就直接break了。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int numUniqueEmails(String[] emails) {
     3         Set<String> set = new HashSet<>();
     4         for (String email : emails) {
     5             int at = email.indexOf("@");
     6             String domain = email.substring(at + 1);
     7             String name = email.substring(0, at);
     8             StringBuilder realName = new StringBuilder();
     9             for (int i = 0; i < name.length(); i++) {
    10                 if (name.charAt(i) == '+') {
    11                     break;
    12                 } else if (name.charAt(i) == '.') {
    13                     continue;
    14                 } else {
    15                     realName.append(name.charAt(i));
    16                 }
    17             }
    18             realName = realName.append("@").append(domain);
    19             set.add(realName.toString());
    20         }
    21         return set.size();
    22     }
    23 }

    LeetCode 题目总结

  • 相关阅读:
    vux 使用 loading 组件
    vux 使用 font-awesome
    批处理常用符号详解
    jQuery.parseJSON vs JSON.parse
    MVC view操作(Razor语法)
    原生JavaScript技巧大收集
    .Net实现表达式计算(公式) 表达式字符串
    .Net文档下载
    MVC下载文档
    .Net实现Word文档及导出
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12970158.html
Copyright © 2011-2022 走看看