码迷,mamicode.com
首页 > 其他好文 > 详细

929. Unique Email Addresses

时间:2019-02-10 00:08:41      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:inpu   receive   note   bst   ddr   sse   eve   ide   ignore   

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);
}

929. Unique Email Addresses

标签:inpu   receive   note   bst   ddr   sse   eve   ide   ignore   

原文地址:https://www.cnblogs.com/17bdw/p/10358160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!