标签:inpu receive note bst ddr sse eve ide ignore
做一个 leetcode 的算法题
https://leetcode.com/problems/unique-email-addresses/
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.
试题是希望过滤每个邮件中的【.】符号,忽略第一个【+】后面的所有内容。
在网上搜索到用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)
设定一个集合变量,先是提取出邮件名与域名,然后过滤掉所有的【.】号。选择所有加号前的第一个索引值,然后把邮件名和域名合并在一起加入集合变量里。返回集合变量的数值就是实际发送数量。
用C++做起来要复杂一点。
#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);
}
标签:inpu receive note bst ddr sse eve ide ignore
原文地址:https://www.cnblogs.com/17bdw/p/10358160.html