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. SQL之总结(二)

    4.关于取两个日期之间的年份: ceil(MONTHS_BETWEEN(sysdate, c.sendtime)/12) workTime ceil(n) 取大于等于n的最小整数 floor(n) 取 ...

  2. P7683 [COCI2008-2009#5] KRUSKA

    洛谷上这道题的第一篇题解.上海加油. 题目大意 Aladdin 已经厌倦了宫殿里的生活.他有一份稳定的工作,他的妻子 Jasmine 和孩子们都在路上,生活变得单调.在这一切的驱使下,他决定在安顿下来 ...

  3. 文档声明(Doctype)和<!Doctype html>有何作用? 严格模式与混杂模式如何区分?它们有何意义?

    文档声明的作用: 文档声明是为了告诉浏览器,当前HTML文档使用什么版本的HTML来写的,这样浏览器才能按照声明的版本来正确的解析. <!doctype html> 的作用就是让浏览器进入 ...

  4. tkinter GUI编程

    tkinter编程概述 tkinter模块包含在Python的基本安装包中.使用tkinter模块编写的GUI程序是跨平台的.可在windows.UNIX.Linux以及Macintonsh OS X ...

  5. python并发——生产者消费者信号量实现

    介绍 写扫描器的时候,需要让资产扫描结果一出来(生产者),另外一边就会开个线程去运行漏洞扫描(消费者). 但是又不能让结果没出来,另外一边消费者就开始干活了. 代码 # *coding:UTF-8 * ...

  6. 【论文阅读】ICLR 2022: Scene Transformer: A unified architecture for predicting future trajectories of multiple agents

    ICLR 2022: Scene Transformer: A unified architecture for predicting future trajectories of multiple ...

  7. Bootstarp框架用法

    Bootstrap框架 Bootstrap框架 2.X 3.X 4.X # 推荐使用3.X版本 使用框架调整页面样式一般都是操作标签的class属性即可 bootstrap需要依赖于jQuery才能正 ...

  8. 约定编程与Sping AOP

    一.约定编程 Spring AOP是一种约定流程的编程,咱们可以先通过动态代理模式的实现来理解Spring AOP的概念. 代理的逻辑很简单,例如,当你需要采访一名儿童时,首先需要经过他父母的同意,在 ...

  9. 可怕!CPU暗藏了这些未公开的指令!

    大家好,我是轩辕. 我们知道,我们平时编程写的高级语言,是经过编译器编译以后,变成了CPU可以执行的机器指令: 而CPU能支持的指令,都在它的指令集里面了. 很久以来,我都在思考一个问题: CPU有没 ...

  10. ArcGIS使用技巧(一)——数据存储

    新手,若有错误还请指正! 日常接触ArcGIS较多,发现好多人虽然也在用ArcGIS,但一些基础的小技巧并不知道,写下来希望对大家有所帮助. ArcGIS默认的存储数据库是在C盘(图1),不修改存储数 ...