码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode811

时间:2018-09-28 00:01:33      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:length   style   return   访问   amp   second   map   count   push   

class Solution {
public:
    void SplitString(const string& s, vector<string>& 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<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> X;
        map<string, int> MAP;
        for (auto cpdomain : cpdomains)
        {
            vector<string> v1;
            SplitString(cpdomain, v1, " ");//将访问数字和域名分开
            int count = atoi(v1[0].c_str());
            string domain = v1[1];

            vector<string> v2;
            SplitString(domain, v2, ".");
            string last = "";
            for (int i = v2.size() - 1; i >= 0; i--)
            {
                if (last == "")
                {
                    last = v2[i];
                }
                else
                {
                    last = v2[i] + "." + last;
                }
                if (MAP.find(last) != MAP.end())//找到了此项目
                {
                    MAP[last] += count;
                }
                else//未有此项
                {
                    MAP.insert(make_pair(last, count));
                }
            }
        }
        for (auto m : MAP)
        {
            string x = m.first;
            stringstream ss;
            ss << m.second;
            string ct = ss.str();
            X.push_back(ct + " " + x);
        }
        return X;
    }
};

 

leetcode811

标签:length   style   return   访问   amp   second   map   count   push   

原文地址:https://www.cnblogs.com/asenyang/p/9716105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!