LeetCode 929 Unique Email Addresses 解题报告
Posted yao1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 929 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.
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?
Input: ["[email protected]","[email protected]","[email protected]"]
?Output: 2
?Explanation: "[email protected]" and "[email protected]" actually receive mails
1 <= emails[i].length <= 100
1 <= emails.length <= 100
?
Each emails[i]
contains exactly one ‘@‘
character.
题目分析及思路
题目要求得到一组邮件地址中不重复的地址的个数,规则是用户名部分有‘.’,则和无‘.’的用户名一样;用户名部分有‘+’,则忽略第一个‘+’后面的部分。两个规则可以同时生效,但对域名部分不起作用。所以先对邮件进行拆分,分为用户名部分和域名部分;之后先找到‘+’,去掉‘+’及后面部分,再去掉‘.’。最后再和‘@’及域名连接。因为要去重,所以使用一个set保存所有不重复的结果。
python代码?
class Solution:
def numUniqueEmails(self, emails):
"""
:type emails: List[str]
:rtype: int
"""
emailsset = set()
for e in emails:
local,domain = e.split("@")
local = local.split("+")[0].replace(".","")
emailsset.add(local+"@"+domain)
return len(emailsset)
以上是关于LeetCode 929 Unique Email Addresses 解题报告的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 929. Unique Email Addresses
[LeetCode] 929. Unique Email Addresses 独特的邮件地址
LeetCode --- 929. Unique Email Addresses 解题报告