LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签: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 (子域名访问计数)的更多相关文章
- Leetcode811.Subdomain Visit Count子域名访问计数
一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有"leetcode.com ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- Java实现 LeetCode 811 子域名访问计数 (暴力)
811. 子域名访问计数 一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有" ...
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- [Swift]LeetCode811. 子域名访问计数 | 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&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
随机推荐
- Python3.4的Pillow库实现验证码图片
转自 http://blog.csdn.net/bin381/article/details/41969493 from PIL import Image,ImageDraw, ImageFont, ...
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- python学习笔记(2)——练习小程序之 " input " 隐藏陷阱
练习小程序之 ----------" input " 隐藏陷阱 age=input('please enter your age:') if age>=18: print(' ...
- JSP学习笔记 - 内置对象 Response
1.response.addHeader("refresh","2"): 制定页面刷新时间 2.response.addHeader("refresh ...
- 常用Linux命令(长期更新)
有些命令如果不常用,老是记不住,每每用到总还要去查,特此将一些命令记录在此: (0)按指定时间删除文件 find target_dir -type f -mtime +3 -exec rm {} \; ...
- 17Web应用乱码问题
Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...
- 第二节:SQLServer导出-重置sa密码-常用sql语句
1.SQLServer导出: 点击要导出数据库----->右键(任务)----->生成脚本----->下一步----->下一步(高级)要编写脚本的数据类型---选择架构和数据 ...
- A5. JVM 如何判断GC对象
[概述] 在堆里面存放着 Java 世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还 “存活” 着,哪些已经 “死去”(即不可能再被任何途径使用的对象). ...
- 数据结构与算法(3)- C++ STL与java se中的vector
声明:虽然本系列博客与具体的编程语言无关.但是本文作者对c++相对比较熟悉,其次是java,所以难免会有视角上的偏差.举例也大多是和这两门语言相关. 上一篇博客概念性的介绍了vector,我们有了大致 ...
- 简述Centos系统启动流程
1. Centos5 POST开机自检 运行CMOS中的BIOS程序,加载第一个启动磁盘的Bootloader 由Bootloader读取kernel 通过挂载临时根目录initramfs加载核心模块 ...