摘要:題目初始算法測(cè)試提交優(yōu)化算法利用的去重利用正則和是一個(gè)特殊字符所以需要轉(zhuǎn)義符代表在之后的所有字符代表結(jié)尾代表或代表在全局用替代開(kāi)始到結(jié)尾的字符和
1.題目
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.
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, "[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?
Example 1:
Input: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" 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)
測(cè)試提交 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.
優(yōu)化算法
利用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.+是一個(gè)特殊字符所以需要轉(zhuǎn)義符
2..*代表在+之后的所有字符
3.$代表結(jié)尾
4.|代表或
5.replace(/+.*$|./g, "")代表在全局用""替代+開(kāi)始到結(jié)尾的字符和.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/102956.html
摘要:題目初始算法測(cè)試提交優(yōu)化算法利用的去重利用正則和是一個(gè)特殊字符所以需要轉(zhuǎn)義符代表在之后的所有字符代表結(jié)尾代表或代表在全局用替代開(kāi)始到結(jié)尾的字符和 1.題目Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], ...
摘要:題目鏈接題目分析題目要求過(guò)濾重復(fù)的郵箱地址。最終返回不重復(fù)的用戶名個(gè)數(shù)。域名部分則不進(jìn)行處理。替換為空字符串。下標(biāo)為用戶名部分,下標(biāo)為域名部分刪去后面的所有字符。最后,用包住以上代碼,在外面初始化數(shù)組,用去重,再該數(shù)組就完成了。 929. Unique Email Addresses 題目鏈接 929. Unique Email Addresses 題目分析 題目要求過(guò)濾重復(fù)的郵箱地址...
摘要:蘋(píng)果郵箱收件服務(wù)器主機(jī)名填什么您好,如果是給手機(jī)設(shè)置郵箱。符號(hào)的左側(cè)是對(duì)方的登錄名,右側(cè)是完整的主機(jī)名,由主機(jī)名和域名組成。其中,域名由幾個(gè)部分組成,每個(gè)部分稱(chēng)為一個(gè)子域,子域之間用圓點(diǎn)隔開(kāi),每個(gè)子域?qū)⒏嬖V用戶一些有關(guān)該郵件服務(wù)器的信息。蘋(píng)果qq郵箱收件服務(wù)器主機(jī)名填什么?您好,如果是給手機(jī)設(shè)置郵箱。您使用手機(jī)郵箱客戶端之前,請(qǐng)確保您是否開(kāi)通了imapsmtppop3郵箱服務(wù),如果沒(méi)有請(qǐng)到網(wǎng)...
摘要:協(xié)議接收郵件服務(wù)器,使用,端口號(hào)。發(fā)送郵件服務(wù)器,使用,端口號(hào)蘋(píng)果郵箱收件服務(wù)器主機(jī)名填什么您好,如果是給手機(jī)設(shè)置郵箱。郵箱的服務(wù)器端口號(hào)為,收件服務(wù)器端口號(hào)是,發(fā)件服務(wù)器端口號(hào)是最后點(diǎn)擊完成。郵件主機(jī)名該如何填寫(xiě)?以qq為例,收件服務(wù)器為imap.qq.com,發(fā)件服務(wù)器為smtp.qq.com,具體如下:1.在郵箱中啟用IMAP服務(wù)。 在Mail中創(chuàng)建一個(gè)新的QQ郵箱賬戶,單擊手動(dòng)設(shè)置進(jìn)...
閱讀 2347·2023-04-25 14:29
閱讀 1484·2021-11-22 09:34
閱讀 2716·2021-11-22 09:34
閱讀 3398·2021-11-11 10:59
閱讀 1866·2021-09-26 09:46
閱讀 2239·2021-09-22 16:03
閱讀 1930·2019-08-30 12:56
閱讀 488·2019-08-30 11:12