前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1.过滤邮箱地址

1.过滤邮箱地址

作者头像
py3study
发布2020-01-06 17:42:35
8250
发布2020-01-06 17:42:35
举报
文章被收录于专栏:python3python3

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.

  1. 初始算法
代码语言:javascript
复制
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.

  1. 优化算法

利用python的set去重

代码语言:javascript
复制
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

代码语言:javascript
复制
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, '')代表在全局用''替代+开始到结尾的字符和.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档