811. Subdomain Visit Count - LeetCode
Question

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.
Solution
题目大意:统计访问子域名的次数
思路:遍历每个域名及其子域名,将其访问次数保存到map里
Java实现:
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> map = new HashMap<>();
for (String str : cpdomains) {
String[] arr = str.split(" ");
int baseCount = Integer.parseInt(arr[0]);
String tmpSub = arr[1];
int fromIndex = 0;
while (fromIndex != -1) {
// System.out.println(fromIndex);
String subDomain = tmpSub.substring(fromIndex + (fromIndex != 0 ? 1 : 0));
Integer oldCount = map.get(subDomain) == null ? 0 : map.get(subDomain);
map.put(subDomain, oldCount + baseCount);
fromIndex = tmpSub.indexOf(".", fromIndex + 1);
}
}
List<String> retList = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
retList.add(entry.getValue() + " " + entry.getKey());
}
return retList;
}
811. Subdomain Visit Count - LeetCode的更多相关文章
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- [LeetCode&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- 【LeetCode】811. Subdomain Visit Count 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- 811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- C#LeetCode刷题之#811-子域名访问计数(Subdomain Visit Count)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3814 访问. 一个网站域名,如"discuss.lee ...
随机推荐
- SQL之总结(三)
1.怎么在where指定多个值得问题? select * from tb_article where article_id in(10008,10009) 结果如下: 如果是字符串的话: select ...
- 利用Charles做代理测试电脑上写的H5页面
做H5页面的同学可能经常会遇到一个场景,就是电脑上调试好的页面怎么在手机上访问测试呢? 下面就介绍一种自己经常使用的方式,利用Charles代理软件来实现! 安装Charles 直接去官网下载对应的系 ...
- Java简单登录图形界面
本文参考与:https://blog.csdn.net/wyf2017/article/details/78831744 https://blog.csdn.net/MengKun822/articl ...
- Windows测试Hadoop报错解决
错误1:HADOOP_HOME and hadoop.home.dir are unset 原因:没有在Windows配置环境变量 解决办法:配置环境变量:记得配置到bin目录 错误2:Could n ...
- 安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?
@dimen/activity_vertical_margin 是什么意思? @dimen/activity_vertical_margin这个的意思就是在你的values文件夹下面的dimens文件 ...
- css让文字显示特定行数,多余的显示省略号
/*css*/ .p{ width: 200px; word-break: break-all; text-overflow: ellipsis; display: -webkit-box; /** ...
- 【C++】二叉树的遍历(前中后)- 迭代法
力扣题目:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 今天自己琢磨了很久如何不用递归将二叉树的遍历写出来,于是乎写出 ...
- caioj 1002: [视频]实数运算2[水题]
题意:输入三个数,计算并输出它们的平均值以及三个数的乘积,结果保留2位小数. 题解:简单题不写题解了-- 代码: #include <cstdio> double a, b, c; int ...
- SpringMVC的数据响应方式-页面跳转
1.返回字符串形式 直接返回字符串:此种方式会返回字符串与视图解析器的前后缀拼接后跳转 有关视图解析器的拼接请访问此地址 注意:WEB-INF下的资源一般不能访问,因为转发是服务器的操作所以可以访问到 ...
- C++ 限定符Const和指针
指向常量的指针 指向常量的指针不能用于其所指对象的值.若想存放常量对象的地址,只能使用指向常量的指针. const int a = 3; //a是个常量,其值不能改变 int *b = &a; ...