题目标签:HashMap

  题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数。

  遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为value。

  最后遍历keyset,把计数和域名重新组合加入result,具体请看code。

Java Solution:

Runtime: 7 ms, faster than 99.54%

Memory Usage: 36.4 MB, less than 99.04%

完成日期:03/15/2019

关键点:hashmap

class Solution
{
public List<String> subdomainVisits(String[] cpdomains)
{
// saving each domains including subdomain into map with its count
Map<String, Integer> map = new HashMap<>();
List<String> result = new ArrayList<>();
// iterate each domain
for(String str : cpdomains)
{
int index = str.indexOf(" ");
// get count frist
int count = Integer.parseInt(str.substring(0, index));
// get domain and its subdomains
String domains = str.substring(index + 1); do {
index = domains.indexOf("."); map.put(domains, map.getOrDefault(domains, 0) + count); if(index > 0) // means still having more subdomain
{
domains = domains.substring(index + 1);
} } while(index > 0); // if index == -1, it means reaching to the end
} for(String str : map.keySet())
{
result.add(map.get(str) + " " + str);
} return result;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 811. Subdomain Visit Count (子域名访问计数)的更多相关文章

  1. Leetcode811.Subdomain Visit Count子域名访问计数

    一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有"leetcode.com ...

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

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

  3. LeetCode 811 Subdomain Visit Count 解题报告

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

  4. 811. Subdomain Visit Count - LeetCode

    Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...

  5. Java实现 LeetCode 811 子域名访问计数 (暴力)

    811. 子域名访问计数 一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有" ...

  6. 【Leetcode_easy】811. Subdomain Visit Count

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

  7. [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count

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

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

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

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

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

随机推荐

  1. Python 快排[pythonnic]

    def QS(array): less = [] more = [] if len(array) <= 1: return array head = array.pop() for x in a ...

  2. dede其他栏目页的logo没有完整显示怎么办?

    在首页完全没有问题,可是点击关于我们.联系我们.加入我们的时候logo图标是缺失的,这时候怎么办? 其实这个是css样式的问题,只要找到相对应页面的css,改一下他们的宽就可以了,如果高不够就自己调整 ...

  3. python学习笔记(2)——练习小程序之 " input " 隐藏陷阱

    练习小程序之 ----------" input " 隐藏陷阱 age=input('please enter your age:') if age>=18: print(' ...

  4. 基于Crypto++的aes 字符串加解密实现

    esaes.h: #ifndef ESAES_H #define ESAES_H #include <cryptopp/aes.h> #include <iostream> # ...

  5. Discuz伪静态代码

    <?php /** * [伪静态跳转(xugui_redirect.{modulename})] (C)2012-2099 Powered by 懒人V难人. * Version: 1.0 * ...

  6. Manjaro安装配置美化字体模糊发虚解决记录

    Manjaro安装记录 前言: ​ 记录自己Manjaro18安装的一些坑,避免下次满互联网找解决方法.在此之前试过Manjaro.Ubuntu.Fedora.linux Mint系统的pac.yum ...

  7. ArrayList经典Demo

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListDemo { public static vo ...

  8. Bullet:关于ORACLE中的HASH JOIN的参数变化

    Oracle在7.3引入了hash join. 但是在Oracle 10g及其以后的Oracle数据库版本中,优化器,实际是CBO,也是因为HASH JOIN仅适用于CBO,在解析目标SQL时是否考虑 ...

  9. 原生js实现三个按钮绑定三个计时器,点击其中一个按钮,开启当前计时器,另外另个不开启

    今天在某个前端交流群,有个小伙伴问了一个小功能,自己想了一下,代码如下,可以实现基本功能: 下面是html结构 <div id="demo"> <input ty ...

  10. 【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)

    http://blog.csdn.net/stationxp/article/details/26077439 计划: 装一个虚机,ubuntu吧,14.04 Trusty Tahr. 安装Mongo ...