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 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: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

Note:

1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one '@' character.
2)answer

试题是希望过滤每个邮件中的【.】符号,忽略第一个【+】后面的所有内容。

在网上搜索到用Python的题解:

class Solution(object):
    def numUniqueEmails(self, emails):
        unique_emails = set()
        
        for email in emails:
            local_name, domain_name = email.split('@')
            local_name = local_name.replace('.', '')
            local_name = local_name.split('+')[0]
            modified_email = local_name + '@' + domain_name
            unique_emails.add(modified_email)
            
        return len(unique_emails)

设定一个集合变量,先是提取出邮件名与域名,然后过滤掉所有的【.】号。选择所有加号前的第一个索引值,然后把邮件名和域名合并在一起加入集合变量里。返回集合变量的数值就是实际发送数量。

3)solution

用C++做起来要复杂一点。

  • 1、提取【@】符号前面的内容,得到邮件名
  • 2、替换掉所有【.】符号,得到实际邮件名
  • 3、提取出【+】符号前的内容,得到发送的邮件名
  • 4、将邮件名与域名合并成实际发送的邮件地址
#include "pch.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using std::string;
using std::vector;
using std::set;


class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        //集合中包含的元素值是唯一的。
        set<string> email_sets;
        for (string email : emails)
        {
            // 提取邮件名
            int pos = email.find('@');
            string local_name = email.substr(0, pos);
            // 过滤掉【.】符号
            // remove:从给定范围中消除指定值,而不影响剩余元素的顺序,并返回不包含指定值的新范围的末尾。
            // 从string中删除所有某个特定字符
            local_name.erase(std::remove(local_name.begin(), local_name.end(), '.'), local_name.end());
            // 提取【+】符号前的字符
            pos = local_name.find('+');
            local_name = local_name.substr(0, pos);
            // 提取【@】后的域名
            pos = email.find('@');
            string domain_name = email.substr(pos + 1);
            // 合并实际发送的邮件名称
            email = local_name + '@' + domain_name;
            // 写进集合中
            email_sets.insert(email);

        }
        // 返回集合大小
        return email_sets.size();
    }
};


int main()
{ 
    //vector初始化字符串
    vector<string> emails;

    emails.push_back("test.email+alex@leetcode.com");
    emails.push_back("test.e.mail+bob.cathy@leetcode.com");
    emails.push_back("testemail+david@lee.tcode.com");

    // 使用内容
    Solution nSolution;
    nSolution.numUniqueEmails(emails);
}

转载于:https://www.cnblogs.com/17bdw/p/10358160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值