1.过滤邮箱地址

49次阅读

共计 2471 个字符,预计需要花费 7 分钟才能阅读完成。

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: 2Explanation: “testemail@leetcode.com” and “testemail@lee.tcode.com” actually receive mails
Note:
1 <= emails[i].length <= 1001 <= emails.length <= 100Each 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 submissionsfor Unique Email Addresses. Memory Usage: 13.2 MB, less than 5.79% ofPython3 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, ”) 代表在全局用 ” 替代 + 开始到结尾的字符和.

正文完
 0