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

811. Subdomain Visit Count - LeetCode的更多相关文章

  1. 【Leetcode_easy】811. Subdomain Visit Count

    problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...

  2. LeetCode 811 Subdomain Visit Count 解题报告

    题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...

  3. [LeetCode&Python] Problem 811. Subdomain Visit Count

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  4. 【LeetCode】811. Subdomain Visit Count 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...

  5. LeetCode 811. Subdomain Visit Count (子域名访问计数)

    题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...

  6. 811. Subdomain Visit Count (5月23日)

    解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...

  7. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  8. [LeetCode] Subdomain Visit Count 子域名访问量统计

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  9. C#LeetCode刷题之#811-子域名访问计数​​​​​​​(Subdomain Visit Count)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3814 访问. 一个网站域名,如"discuss.lee ...

随机推荐

  1. html5手机页面的那些meta

    一.普通手机页的设置1.<meta name="viewport" content=""/>说明:屏幕的缩放 content的几个属性: width ...

  2. 小程序开发之一(使用fly进行http封装)

    原文地址:http://callmesoul.cn 下载fly js文件 fly小程序文档 /api/config.js 配置,主要配置全局的host url和request拦截和request拦截 ...

  3. React系列——websocket群聊系统在react的实现

    前奏 这篇文章仅对不熟悉在react中使用socket.io的人.以及websocket入门者有帮助. 下面这个动态图展示的聊天系统是用react+express+websocket搭建的,很模糊吧, ...

  4. JDK1.8.0_181的无限制强度加密策略文件变动(转载)

    JDK1.8.0_181的无限制强度加密策略文件变动 原文地址 https://my.oschina.net/my1313677/blog/3109613 作者 葉者 日常记录 2019/09/23 ...

  5. random模块、os模块、序列化模块、sy模块s、subprocess模块

    random随机数模块 random.random( ) 随机产生一个0-1之间的小数 print(random.random()) # 0.31595547439342897 random.rand ...

  6. linux centos 8.2 安装docker

    1 使用yum -y install docker安装后启动docker提示Failed to start docker.service: Unit docker.service not found. ...

  7. java过滤器拦截器的执行时机

    https://www.cnblogs.com/shamo89/p/8534580.html https://www.cnblogs.com/juanzila/p/11276067.html

  8. 7.Arrays类

    1. Arrays类 数组的工具类java.util.Arrays Arrays类中的方法都是static修饰的静态方法,使用的时候可以直接使用类名进行调用 (而不是使用对象)(是"不用&q ...

  9. MPU9250/MPU6050与运动数据处理与卡尔曼滤波(1)

    第一篇--概述和MPU6050及其自带的DMP输出四元数 概述 InvenSense(国内一般译为应美盛)公司产的数字运动传感器在国内非常流行,我用过它的两款,9250和6050.出于被国产芯片惯坏的 ...

  10. JDK7u21反序列链学习

    JDK7u21 1.前置知识 jdk7u21是一条不依赖CommonsCollections库依赖的,看利用链所有知识其实跟CommonsCollections也有重复,我们来学习一下以前没学过的类或 ...