资讯专栏INFORMATION COLUMN

1.过滤邮箱地址

caiyongji / 690人阅读

摘要:题目初始算法测试提交优化算法利用的去重利用正则和是一个特殊字符所以需要转义符代表在之后的所有字符代表结尾代表或代表在全局用替代开始到结尾的字符和

1.题目
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:

1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one "@" character.

初始算法

class Solution:
    def numUniqueEmails(self, emails: List[str]) -> int:
        n = len(emails)
        final_list = []
        for i in range(0, n):
            split_list =  emails[i].split("@")
            local_name, domain_name = split_list[0].split("."), split_list[1]
            local_name = "".join(local_name).split("+")[0]
            final_name = local_name + "@" + domain_name
            if(final_name not in final_list):
                final_list.append(final_name)        
        return len(final_list)
测试提交 Runtime: 52 ms, faster than 55.04% of Python3 online submissions
for Unique Email Addresses. Memory Usage: 13.2 MB, less than 5.79% of
Python3 online submissions for Unique Email Addresses.

优化算法

利用python的set去重

class Solution:
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        email_set = set()
        for email in emails:
            local_name,domain_name = email.split("@")
            local_name ="".join(local_name.split("+")[0].split("."))
            email = local_name +"@" + domain_name
            email_set.add(email)
        return len(email_set)

Js利用正则和Set

const numUniqueEmails = emails => new Set(emails.map(mail => `${mail.split("@")[0].replace(/+.*$|./g, "")}@${mail.split("@")[1]}`)).size

is a specail char, so adds .

.* means any character after +.
$, in regex, it represents the end of string.
| equals to or.
. is also a special char, so adds .
In the end of regex, g, global search, means finding all matches in input and replace them.
In sum, replace the substring that after sign + or the char . with empty string.

1.+是一个特殊字符所以需要转义符
2..*代表在+之后的所有字符
3.$代表结尾
4.|代表或
5.replace(/+.*$|./g, "")代表在全局用""替代+开始到结尾的字符和.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/102956.html

相关文章

  • 1.过滤邮箱地址

    摘要:题目初始算法测试提交优化算法利用的去重利用正则和是一个特殊字符所以需要转义符代表在之后的所有字符代表结尾代表或代表在全局用替代开始到结尾的字符和 1.题目Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, ...

    littleGrow 评论0 收藏0
  • Leetcode PHP题解--D2 929. Unique Email Addresses

    摘要:题目链接题目分析题目要求过滤重复的邮箱地址。最终返回不重复的用户名个数。域名部分则不进行处理。替换为空字符串。下标为用户名部分,下标为域名部分删去后面的所有字符。最后,用包住以上代码,在外面初始化数组,用去重,再该数组就完成了。 929. Unique Email Addresses 题目链接 929. Unique Email Addresses 题目分析 题目要求过滤重复的邮箱地址...

    xuhong 评论0 收藏0
  • 登陆邮箱主机名什么意思-苹果qq邮箱收件服务器主机名填什么?

    摘要:苹果邮箱收件服务器主机名填什么您好,如果是给手机设置邮箱。符号的左侧是对方的登录名,右侧是完整的主机名,由主机名和域名组成。其中,域名由几个部分组成,每个部分称为一个子域,子域之间用圆点隔开,每个子域将告诉用户一些有关该邮件服务器的信息。苹果qq邮箱收件服务器主机名填什么?您好,如果是给手机设置邮箱。您使用手机邮箱客户端之前,请确保您是否开通了imapsmtppop3邮箱服务,如果没有请到网...

    SegmentFault 评论0 收藏0
  • 收件服务器主机名应该填写什么-邮件主机名该如何填写?

    摘要:协议接收邮件服务器,使用,端口号。发送邮件服务器,使用,端口号苹果邮箱收件服务器主机名填什么您好,如果是给手机设置邮箱。邮箱的服务器端口号为,收件服务器端口号是,发件服务器端口号是最后点击完成。邮件主机名该如何填写?以qq为例,收件服务器为imap.qq.com,发件服务器为smtp.qq.com,具体如下:1.在邮箱中启用IMAP服务。 在Mail中创建一个新的QQ邮箱账户,单击手动设置进...

    habren 评论0 收藏0

发表评论

0条评论

caiyongji

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<