811. Subdomain Visit Count - LeetCode
Question

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的更多相关文章
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- [LeetCode&Python] Problem 811. 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 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- 811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- C#LeetCode刷题之#811-子域名访问计数(Subdomain Visit Count)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3814 访问. 一个网站域名,如"discuss.lee ...
随机推荐
- Pandas查询数据的几种方法
Pandas查询数据 Pandas查询数据的几种方法 df.loc方法,根据行.列的标签值查询 df.iloc方法,根据行.列的数字位置查询 df.where方法 df.query方法 .loc既能查 ...
- 记离线缓存(manifest)一大坑,断定其只适用于静态网站或离线应用
今天看了离线缓存(manifest)方面的资料,兴冲冲地就想给自己的网站用上.待我把代码都写好部署上服务器,并测试过OK的时候,在SegmentFault刷了一把manifest方面的问答,才发现这个 ...
- Android点击按钮退出程序并提醒
效果展示: MainActivity.java import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDia ...
- eclipse/myeclipse注释模板的修改
本文转自:http://kaminlee.iteye.com/blog/1101938 Window --> Java --> Code Style --> Code Templat ...
- 浅谈Web前后端分离的意义
自然是有很大意义的.下面我可能说的比较多--方便题主能够更全面的了解为什么说是有有意义的.另外,本文是以Java的角度谈前后端分离.放心,大家一定会有种是我了,没错,的感觉. 一.先来明晰下概念 前后 ...
- 微信小程序获取当前时间戳、获取当前时间、时间戳加减
//获取当前时间戳 var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log("当前 ...
- 输入n,然后输入n个数,使它升序输出
#include<iostream> using namespace std; int main() { int n,i,j,m,k; cin>>n; int a[n]; f ...
- c++对于c的扩展_冒号作用域
冒号作用域 ::(该运算符为作用域):如果::前面什么都没加代表全局作用域 #include <iostream> using namespace stu; int a=10; viod ...
- python---用顺序表实现栈
class Stack(object): """栈, 存放数据的一种容器, 后进先出""" def __init__(self): self ...
- Ubuntu中hyperledger-fabric2.3.0环境搭建
系统环境 hyperledger-fabric在Ubuntu安装过程,fabric版本为2.3.0 首先安装相关软件 1.安装docker 直接参考下面这篇文档安装好docker-ce即可 Ubunt ...