首页 > 其他 > 详细

leetcode811

时间:2018-09-27 23:45:23      阅读:316      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!