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 ...
随机推荐
- Android集成微信分享功能应用签名生成方法及分享不生效的问题
通过友盟sdk集成微博.微信.qq等分享功能时,微博和qq很顺利,但在做微信集成时一直不成功.主要问题还是之前在微信开放平台申请创建移动应用时,对应用签名没有填写对,走了很多弯路现总结出来,加深记忆避 ...
- Ionic2/angularJs2中的静态类 PhotoLibrary 调用不上
photoLibrary调用报错:No provider for PhotoLibrary: 在调用相册文件时有用到photolibrary,总有些莫名的报错,3月份的时候这个坑让我不知所措,现在写下 ...
- oracle dos命令
1.无账户密码登录数据库:sqlplus/nolog 后面不能加分号,否则不能识别 2.登录数据库:sqlplus 3.在sql下测试连接性:conn oracle_name/oracle_passw ...
- mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- Android Measure 体系简单总结
Android对View的测量是半协商半强制半模糊半具体的. 测量过程中的两套尺寸体系: [半强制] ParentView**约束ChildView: **MeasureSpec(通过measure ...
- 安卓app测试之内存监控
一.通过Dumpsys 来取值 1.adb shell dumpsys meminfo 获取的所有进程的内存信息,以及总内存,剩余内存,使用的内存等信息. 2.想获得某一进程内存的详细信息,在后面加上 ...
- vue中的input使用e.target.value赋值的问题
很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...
- @ExceptionHandler和@ControllerAdvice统一处理异常
//@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...
- relax 网站
1. Calm 网站链接:http://www.calm.com/ 这个网站就像它的名字一样“平和”,网站的设计是通过自然图片(阳光下的暖流.流淌的消息等)与缓缓的音乐相结合,帮你在短时间内即可放松下 ...
- angular5中的自定义指令(属性指令)
属性型指令用于改变一个 DOM 元素的外观或行为. 在 Angular 中有三种类型的指令: 组件 — 拥有模板的指令 结构型指令 — 通过添加和移除 DOM 元素改变 DOM 布局的指令 属性型指令 ...