Leetcode811.Subdomain Visit Count子域名访问计数
一个网站域名,如"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) {
int len = cpdomains.size();
map<string, int> save;
for(int i = 0; i < len; i++)
{
int cnt = 0;
int size = cpdomains[i].size();
for(int j = 0; j < size; j++)
{
if(cpdomains[i][j] == ' ')
break;
cnt = cnt * 10 + cpdomains[i][j] - '0';
}
string temp = "";
for(int j = size - 1; j >= 0; j--)
{
if(cpdomains[i][j] == '.')
{
save[temp] += cnt;
temp = cpdomains[i][j] + temp;
}
else if(cpdomains[i][j] == ' ')
{
save[temp] += cnt;
break;
}
else
{
temp = cpdomains[i][j] + temp;
}
}
}
vector<string> res;
for(map<string, int> :: iterator itr = save.begin(); itr != save.end(); itr++)
{
int temp = itr ->second;
string str = "";
while(temp)
{
int x = temp % 10;
str = (char)(x + '0') + str;
temp /= 10;
}
str = str + ' ' + itr ->first;
res.push_back(str);
}
return res;
}
};
Leetcode811.Subdomain Visit Count子域名访问计数的更多相关文章
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- Java实现 LeetCode 811 子域名访问计数 (暴力)
811. 子域名访问计数 一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有" ...
- [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- LeetCode子域名访问计数-Python3.7<五>
上一篇:LeetCode 键盘行<四> 题目:https://leetcode-cn.com/problems/subdomain-visit-count/description/ 一个网 ...
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- C#LeetCode刷题之#811-子域名访问计数(Subdomain Visit Count)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3814 访问. 一个网站域名,如"discuss.lee ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
随机推荐
- PAT甲级——A1048 Find Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- java 异常捕获小记
java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...
- MATLAB---fopen、fprintf函数
1 概述 fopen()是个将数据按指定格式读入到matlab中的函数. fprintf()是个将数据按指定格式写入到文本文件中的函数. 2 用法 2.1 fopen函数 matlab中fopen函数 ...
- tp5 报 A non well formed numeric value encountered 的错解决办法
thinkphp5出现A non well formed numeric value encountered的解决办法修改formatDateTime方法如下 默认值: if (is_null($th ...
- JAVA面试常见问题之基础篇
一. 面向对象的特征:继承.封装.(抽象).多态 继承:继承是子类自动共享父类数据和方法的机制,这是类之间的一种关系,提高了软件的可重用性和可扩展性. 封装:封装是保证软件部件具有优良的模块性的基础 ...
- (5)连续非周期信号的傅里叶变换(频谱) & 周期信号的傅里叶变换
参考资料:<信号与系统(第二版)> 杨晓非 何丰 从傅里叶级数到傅里叶变换 通过分析连续周期信号的周期与频谱的关系,当周期趋于无穷大的时候,周期信号变成非周期信号.从频谱分析观点来看,当T ...
- 洛谷1850(NOIp2016) 换教室——期望dp
题目:https://www.luogu.org/problemnew/show/P1850 状态里记录的是”上一回有没有申请“,而不是”上一回申请成功否“,不然“申请 j 次”就没法转移了. dou ...
- Ubuntu18.04 磁盘挂载在某目录下
简介 记录Ubuntu18.04 桌面版系统下实现某个磁盘挂载到自己想要的目录下,内容参考网上教程,此处为自己操作记录. 查看当前所有的磁盘信息 命令:sudo fdisk -l 从列出的信息中可以看 ...
- java如何使用 tesseract 4.0.0-1.4.4
提示: 建议直接使用tess4j,tess4j是对tesseract的封装,使用更简单 首先引入依赖 <!-- https://mvnrepository.com/artifact/org.by ...
- nginx链接末尾自动补全斜杠
放在locaation里边就行 if (-d $request_filename){ rewrite ^(.*[^/])$ $/ permanent;#加斜杠 } 这样,nginx就会进行判断了,如果 ...