[LeetCode] 811. Subdomain Visit Count

Easy

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.

Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".

We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

Example 1:
Input: 
["9001 discuss.leetcode.com"]
Output: 
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation: 
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.

Example 2:
Input: 
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output: 
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation: 
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.

Notes:

  • The length of cpdomains will not exceed 100
  • The length of each domain name will not exceed 100.
  • Each address will have either 1 or 2 "." characters.
  • The input count in any count-paired domain will not exceed 10000.
  • The answer output can be returned in any order.

题目大意:一个网站域名由多个子域组成。例如"discuss.leetcode.com",顶层子域是com,下一层为leetcode.com,最底层为discuss.leetcode.com。将这个样式的域称为“计数配对域”:"9001 discuss.leetcode.com",表示访问这个域9001次。

题目给出了计数配对域的列表cpdomains。 求出一个计数配对的域的列表(以与输入相同的格式,并且以任意顺序),该列表明确地计算了每个子域的访问次数。

方法:

暴力求解:维护一个map记录每个子域出现的次数,最后再把这个map的各项转换成计数配对域的形式。因为给出的计数是字符串形式,中间要进行子域的次数累计,最好又要以字符串的形式输出,所以要用到string和int的相互转换。

/* string转int */
string str="1234";
int a=atoi(str.c_str());
/* int转string */
int a=1234;
string str=to_string(a);

只要遍历cpdomains,将相同子域的出现次数相加,最后将所有子域和其出现的次数合并成计数配对域的形式就可以了。

代码如下:

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> res;
        map<string,int> m;
        for(string cpdomain:cpdomains){
            int times=0;
            for(int j=0;j<cpdomain.size();++j){
                if(cpdomain.substr(j,1)==" "){
                    times=atoi(cpdomain.substr(0,j+1).c_str());
                    m[cpdomain.substr(j+1)]+=times;
                }
                else if(cpdomain.substr(j,1)=="."){
                     m[cpdomain.substr(j+1)]+=times;
                }
            }
        }
        
        map<string,int>::iterator it=m.begin();
        while(it!=m.end()){
            string temp=to_string(it->second)+" "+it->first;
            res.push_back(temp);
            it++;
        }
        return res;
    }
};

方法二:思路和上边的一样,只是使用了find函数进行了代码的简化。

代码如下:

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> res;
        map<string,int> m;
        for(string cpdomain:cpdomains){
            int index=cpdomain.find(" ");
            int times=stoi(cpdomain.substr(0,index));
            
            while(index != string::npos){
                m[cpdomain.substr(index+1)]+=times;
                index=cpdomain.find(".",index+1);
            }
        }
        
        for(auto a:m){
            string temp=to_string(a.second)+" "+a.first;
            res.push_back(temp);
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/cff2121/p/11578964.html