Question

811. Subdomain Visit Count

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;
}

最新文章

  1. DOM对象模型接口规范中的四个基本接口
  2. Node异步I/O、事件驱动与高性能服务器
  3. 在stackoverflow上使用markdown
  4. JMeter学习(十九)JMeter测试MongoDB
  5. subeclipse 安装
  6. POJ 1236
  7. mybatis Java API
  8. asynDBcenter(复习)
  9. UVa 10375 (唯一分解定理) Choose and divide
  10. 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
  11. SQL 测试
  12. 由浅入深吃透MVC框架,驯服烂代码
  13. centos 6.X 安装node
  14. Android创建与读取Excel
  15. [转]CentOS_yum的详细使用方法
  16. 80x86汇编小站站长简单介绍-2014年08月23日
  17. 快速构建Windows 8风格应用33-构建锁屏提醒
  18. PHP 上传文件大小限制
  19. REACT相关资料合集
  20. java包

热门文章

  1. led指示灯电路图大全(八款led指示灯电路设计原理图详解)
  2. 基于Vue实现关键词实时搜索高亮显示关键词
  3. java中递归的用法和例子
  4. android的布局xml文件如何添加注释?
  5. js判断json数据是否存在某字段的方法
  6. [MySQL]IP处理函数inet_aton()和inet_ntoa()
  7. C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism
  8. js知识梳理4.继承的模式探究
  9. Hyperledger Fabric的test-network启动过程Bash源码详解
  10. 帝国CMS随机调用栏目