一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。
给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。
接下来会给出一组访问次数和域名组合的列表cpdomains
。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。
示例 1: 输入: ["9001 discuss.leetcode.com"] 输出: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"] 说明: 例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com"和"com"都会被访问,所以它们都被访问了9001次。
示例 2 输入: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] 输出: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] 说明: 按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。 而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。
注意事项:
-
cpdomains
的长度小于100
。 - 每个域名的长度小于
100
。 - 每个域名地址包含一个或两个"."符号。
- 输入中任意一个域名的访问次数都小于
10000
。
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> c;
for (auto cd : cpdomains){
int i = cd.find(" ");
int n = stoi(cd.substr(0, i));
string s = cd.substr(i + 1, cd.size() - i - 1);
for (int i = 0; i < s.size(); ++i)
if (s[i] == '.')
c[s.substr(i + 1, s.size () - i)] += n;
c[s] += n;
}
vector<string> res;
for (auto k : c) res.push_back(to_string(k.second) + " " + k.first);
return res;
}
};
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, unsigned> imap;
//其中string为子域,即在每一个字符串元素中由'.'来分分隔开;unsigned的值为子域出现的频次,不可能小于0,所以定义为无符号型。
for(const string &cpdomain : cpdomains)
//使用范围for循环遍历每一个字符串元素cpdomain,由于对cpdomain只进行读操作,所以定义为const string (auto)型。
{
size_t pos = cpdomain.find(' ');//找到第一个数字后面的空格
string count = string(cpdomain.begin(), cpdomain.begin() + pos);
//在每个字符串元素cpdomain中,子域出现的频次count在最前面且由空格隔开,所以如果找到空格所在的位置pos,则数字字符串位于begin()和begin() + pos之间;
while(1){
pos ++;
imap[string(cpdomain.begin() + pos, cpdomain.end())] += stoi(count);//把对应的关键字映射到频次;stoi()函数把字符串转换为整型
pos = cpdomain.find('.', pos);
if(pos == string::npos) break;//找到'.'的位置,加1操作以后继续对下一个cpdomain进行处理,直到找不到'.';
}
}
vector<string> res; //定义一个字符串型的顺序容器res;
for(const auto &m : imap){
//添加新元素到容器的末尾
res.emplace_back(to_string(m.second));
//使用vector的成员函数emplace_back()每次向res容器的末端添加一个字符串元素(值,使用to_string()把整型转换为字符串);
res.back() += " ";//使用vector的成员函数back()返回容器中的最后一个字符
res.back() += m.first; //使用'+'连接空格和关键字,
//res.emplace_back(" ");
//res.emplace_back(m.first);
}
return res;
}
};
#include<string>
using namespace std;
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
map<string, int> m;
for(int i = 0; i < cpdomains.size(); i++){
int index_ = cpdomains[i].find(' ');
int count = stoi(cpdomains[i].substr(0, index_));
string domain = cpdomains[i].substr(index_+1);
m[domain] += count;
while((index_ = domain.find(".")) != string::npos){
domain = domain.substr(index_+1);
m[domain] += count;
}
}
vector<string> res;
for(const auto& v : m){
res.push_back( to_string(v.second) + " " + v.first);
}
return res;
}
};
还不太懂这道题。。。。。