929. Unique Email Addresses(erase用法和string[]数组初始化)

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.

本来是个水题,结果因为我吧erase用错了,调试了一会才解决问题。

erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
注意第2,3种,参数均是迭代器,不是int类型的。

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
using namespace std;

 string changeEmail(string str){
    string samestr;
    for(int i=0;i<str.length();i++){
        if(str[i]=='.'){
            str.erase(i--,1);
        }
        else if(str[i]=='+'){
            str.erase(i--,str.find('@')-i); //erase的参数
            break;
        }else if(str[i]=='@'){
            break;
        }
    }
    //cout<<str<<endl;
    return str;
}
int numUniqueEmails(vector<string>& emails) {
    set<string> uniemail;
    for(int i=0;i<emails.size();i++){
        uniemail.insert(changeEmail(emails[i]));
    }
    return uniemail.size();
}

int main()
{
    cout << "Hello world!" << endl;

    string str[]={"c.o.t.m.f.q.+rt.i.l@zuvn.skdsq.com","d.c.fw.jc.c.gt+w@yzuq.com","n.kvtb+a.iq.c.l@rupfxkfr.com","xckqzemg+f.h@hbtp.com",
    "mh+f.o+k.d.e.x.fe@gywxo.n.com","d.c.fw.jc.c.gt+q@yzuq.com","hh.a.fm.jea.s+t@epo.com","xckqzemg+dx@hbtp.com","n+gz.fyk+f.oi+e@j.fe.com",
    "vqvp.p.lvt.t+k@rexcs.com","nnwhgqn+qm.n@chmgo.com","mrjrugbrh+e@z.zvnnhswsq.com","mrjrugbrh+s@z.zvnnhswsq.com","mh+fclendxh@gywxo.n.com",
    "t.bgivgn.yh+l@sytfq.mgr.com","cv.oau.d.zsub+x@ybzkk.com","oww.sotdsb.j+w@aadoc.r.com","y.qdv+v+b+bk.di@wd.lvz.com","nnwhgqn+h.se@chmgo.com",
    "q.y+un.d.d.q+n+or@rq.wxd.com"};
    int len = sizeof(str)/sizeof(str[0]);
    vector<string> emails(str,str+len);
    cout<<numUniqueEmails(emails)<<endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值