其他分享
首页 > 其他分享> > 811. Subdomain Visit Count

811. Subdomain Visit Count

作者:互联网

class Solution {
public:
    
    vector<string> split(string s){
        string temp="";
        vector<string> res;
        for(int i=0;i<s.size();i++){
            if (s[i]==' ' || s[i]=='.'){
                if (temp.size()!=0){
                    res.push_back(temp);
                    temp = "";
                }
            }
            else{
                temp += s[i];
            }
        }
        if (temp.size()!=0){
            res.push_back(temp);
        }
        return res;      
    }
    
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        map<string, int> cnt;
        
        for(int i=0;i<cpdomains.size();i++){
            vector<string> ss = split(cpdomains[i]);
            if (ss.size()==3){
                int num = atoi(ss[0].c_str());
                string str1 = ss[1];
                string str2 = ss[2];
                string ss1 = str1 + '.' + str2;
                cnt[ss1] += num;
                cnt[str2] += num;
            }
            else{
                int num = atoi(ss[0].c_str());
                string str1 = ss[1];
                string str2 = ss[2];
                string str3 = ss[3];
                string ss1 = str1 + '.' + str2+'.'+str3;
                cnt[ss1] += num;
                string ss2 = str2+'.'+str3;
                cnt[ss2] += num;
                string ss3 = str3;
                cnt[ss3] += num;
            }
        }
        
        // cour result
        vector<string> res;
        for(auto it=cnt.begin();it!=cnt.end();it++){
            string temp = to_string(it->second) + " " + it->first;
            res.push_back(temp);
        }
        return res;
        
    }
};

标签:Count,cnt,string,ss,str2,int,num,811,Subdomain
来源: https://blog.csdn.net/zeroQiaoba/article/details/104882152