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 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 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?
Example 1:
Input: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" 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("[email protected]");
emails.push_back("[email protected]");
emails.push_back("[email protected]");
// 使用内容
Solution nSolution;
nSolution.numUniqueEmails(emails);
}
原文地址:https://www.cnblogs.com/17bdw/p/10358160.html
时间: 2024-10-12 17:01:12