LeetCode Problem -- 929. Unique Email Addresses

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38088298/article/details/85728598
  • 描述:
    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.

  • 分析:给出多个电子邮件地址,对其进行化简,找出有多少个不同的邮件地址。
    其中有如下三条规则:
    1.在’@'前出现‘.’,就将‘.’删除
    2.在’@'前出现‘+’,则忽视‘+’后面的字符
    3.对’@'后的字符不做任何替换

  • 思路一:对给出的邮件地址进行分段,分为‘+’(或‘@’)之前和 ‘@’之后的部分,对前半部分的‘.’进行删除,将后半部分与删除操作后的前半部分进行拼接,将拼接所得的string存入set中,最终set中的元素个数就是不同的电子邮件数目。

class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string> emails_match;
        string temp_str = "";
        int spilt_pos = 0, unique_pos = 0;
        for (int i = 0 ; i < emails.size(); i++) {
            unique_pos = emails[i].find('@');
            spilt_pos = unique_pos;
            if (emails[i].find('+') != -1) spilt_pos = emails[i].find('+');
            temp_str = emails[i].substr(0, spilt_pos);
            for (int j = 0; j < temp_str.size(); j++) {
                if (temp_str.find('.') == -1) break;
                int pos = temp_str.find('.', j);
                temp_str.erase(pos, 1);
                j = pos;
            }
            temp_str += emails[i].substr(unique_pos, emails[i].size() - unique_pos);
            emails_match.insert(temp_str);
        }
        return emails_match.size();
    }
};
  • 思路二:用remove()函数对替换‘.’操作进行优化
class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string> emails_match;
        string temp_str = "";
        int spilt_pos = 0, unique_pos = 0;
        for (int i = 0 ; i < emails.size(); i++) {
            unique_pos = emails[i].find('@');
            spilt_pos = unique_pos;
            if (emails[i].find('+') != -1) spilt_pos = emails[i].find('+');
            temp_str = emails[i].substr(0, spilt_pos);
            temp_str.erase(remove(temp_str.begin(), temp_str.end(), '.'), temp_str.end());
            temp_str += emails[i].substr(unique_pos, emails[i].size() - unique_pos);
            emails_match.insert(temp_str);
        }
        return emails_match.size();
    }
};

猜你喜欢

转载自blog.csdn.net/m0_38088298/article/details/85728598