LeetCode—811. Subdomain Visit Count


//url:https://leetcode.com/problems/subdomain-visit-count/description/
class Solution {
public:
    void SplitString(const string& s, vector& v, const string& c)
    {
    string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2-pos1));
         
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
        v.push_back(s.substr(pos1));
    }
    
    vector subdomainVisits(vector& cpdomains) {
        map result;
        for(int i=0;i cutstr;
            SplitString(cpdomains[i],cutstr," ");
            int count=atoi(cutstr[0].c_str());
            vector domainstr;
            SplitString(cutstr[1],domainstr,".");
            string tmp=domainstr[domainstr.size()-1];
            map::iterator it=result.find(tmp);
            if(it==result.end()){
                result[tmp]=count;
            }else{
                it->second+=count;
            }
            
            for(int j=domainstr.size()-2;j>=0;j--){
                tmp=domainstr[j]+"."+tmp;
                map::iterator it=result.find(tmp);
                if(it==result.end()){
                    result[tmp]=count;
                }else{
                    it->second+=count;
                }
            }
        }
        vector re;
        for(map::iterator it=result.begin();it!=result.end();++it){
            string tmp=to_string(it->second)+" "+it->first;
            re.push_back(tmp);
        }
        return re;
    }
};

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注