leetcode811

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode811相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 811 子域名访问计数[Map 字符串] HERODING的LeetCode之路

811. Subdomain Visit Count - LeetCode

[leetcode-811-Subdomain Visit Count]

[LeetCode] 811. Subdomain Visit Count

LeetCode 811 Subdomain Visit Count 解题报告

leetcode811