TreeviewCopyright © aleen42 all right reserved, powered by aleen42

929. 独特的电子邮件地址

https://leetcode-cn.com/problems/unique-email-addresses/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-29 23:17:50
 * @LastEditTime: 2020-08-29 23:18:19
 * @Description: https://leetcode-cn.com/problems/unique-email-addresses/
 * @FilePath: \leetcode-googtech\#929. Unique Email Addresses\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for(String email : emails) {
            StringBuilder sb = new StringBuilder();
            // substring(int beginIndex, int endIndex)
            sb.append(email.substring(0, email.indexOf('+') == -1 ? email.indexOf('@') : email.indexOf('+')).replace(".", "")); 
            // substring(int beginIndex)
            sb.append(email.substring(email.indexOf('@'))); 
            set.add(sb.toString());
        }
        return set.size();
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-29 23:17:56
LastEditTime: 2020-08-29 23:18:12
Description: https://leetcode-cn.com/problems/unique-email-addresses/
FilePath: \leetcode-googtech\#929. Unique Email Addresses\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        # 初始化用于存储邮件地址的 Set 集合
        emailSet = set()
        # 逐个遍历数组中的邮件地址
        for email in emails:
            # 分割邮件中的本地名称与域名
            name, domain = email.split('@')
            # 判断本地名称中是否存在符号 '+'
            if name.find('+') == -1: 
                # 本地名称中不存在符号 '+',所以仅删除名称中的符号 '.' 即可
                name = name.replace('.','')
            else: 
                # 忽略本地名称中符号 '+' 后面的字符,并删除名称中的符号 '.' 
                name = name[:name.find('+')].replace('.', '')
            # 重新组合本地名称与域名,并将其存储到 Set 集合中
            emailSet.add(name + '@' + domain)
        # 返回 Set 集合中不同邮件的数量
        return len(emailSet)
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""