[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 |
Every email consists of a local name and a domain name,separated by the @ sign. For example,in? Besides lowercase letters,these emails may contain? If you add periods ( If you add a plus ( It is possible to use both of these rules at the same time. Given a list of? Example 1: Input: ["[email?protected]","[email?protected]","[email?protected]"]
Output: 2 Explanation:?"[email?protected]" and "[email?protected]" actually receive mails
Note:
每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔。 例如,在? 除了小写字母,这些电子邮件还可能包含? 如果在电子邮件地址的本地名称部分中的某些字符之间添加句点( 如果在本地名称中添加加号( 可以同时使用这两个规则。 给定电子邮件列表? 示例: 输入:["[email?protected]","[email?protected]","[email?protected]"] 输出:2 解释:实际收到邮件的是 "[email?protected]" 和 "[email?protected]"。 提示:
412ms 1 class Solution { 2 func numUniqueEmails(_ emails: [String]) -> Int { 3 var es:Set<String> = Set<String>() 4 for e in emails 5 { 6 //分割字符串 7 var s: Array = e.components(separatedBy: "@") 8 var str:String = String(s[0]) 9 //字符串替换 10 str = str.replacingOccurrences(of: ".",with: "") 11 //字符查找,返回字符索引 12 var ind = str.firstIndex(of: "+") ?? s[0].endIndex 13 if ind != str.endIndex 14 { 15 //截取子字符串 16 str = String(str[..<ind]) 17 } 18 //拼接字符串,Set添加用.insert 19 es.insert(str + "@" + s[1]) 20 } 21 return es.count 22 } 23 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |