zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    jQuery 选择器
    jQuery 语法
    jQuery 简介
    JSON 使用
    JSON 语法
    Android——Activity跳转
    Activity
    Activity2.java
    Android——Activity练习
    Andriod——手机尺寸相关的概念 +尺寸单位+关于颜色
  • 原文地址:https://www.cnblogs.com/asenyang/p/9716105.html
Copyright © 2011-2022 走看看