zoukankan      html  css  js  c++  java
  • [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;
        }
    };
  • 相关阅读:
    tomcat源码springboot搭建的跑包含websocket的项目
    tomcat源码ant编译
    职责链设计模式最简单的实例
    完美解决asp.net core 3.1 两个AuthenticationScheme(cookie,jwt)共存在一个项目中
    基于领域驱动设计(DDD)超轻量级快速开发架构(二)动态linq查询的实现方式
    Html5 在手机端 input 默认弹出英文键盘
    Html Table 表格 画斜线
    多个单列索引和联合索引的区别
    springboot常用功能
    前端代码评审(Code Review)
  • 原文地址:https://www.cnblogs.com/cff2121/p/11578964.html
Copyright © 2011-2022 走看看