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)

原文地址:https://www.cnblogs.com/yao1996/p/10198683.html

时间: 2024-10-12 10:26:11

LeetCode 929 Unique Email Addresses 解题报告的相关文章

[leetcode] 929. Unique Email Addresses (easy)

统计有几种邮箱地址. 邮箱名类型:[email protected] 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以及之后的local全部忽略. a+bc=a 思路: 利用set存,水题没啥好说的 Runtime: 20 ms, faster than 96.66% of C++ online submissions for Unique Email Addresses. class Solution { pub

[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. Besides lowercase letters, these emails may contain?'.'s or?'+'s. If you

929. Unique Email Addresses

Algorithm 做一个 leetcode 的算法题 Unique Email Addresses https://leetcode.com/problems/unique-email-addresses/ 1)problem 929. Unique Email Addresses Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email pro

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the

【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. Besides lowercase letters, these emails may contain '.'s or '+'s.

【LeetCode】Restore IP Addresses 解题报告

[题目] Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) [解析] 题意:把一

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa