问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3814 访问。

一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。

给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。

接下来会给出一组访问次数和域名组合的列表cpdomains 。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。

输入: ["9001 discuss.leetcode.com"]

输出: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]

说明: 例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com"和"com"都会被访问,所以它们都被访问了9001次。

输入: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]

输出: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]

说明: 按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。

注意事项:

cpdomains 的长度小于 100。
每个域名的长度小于100。
每个域名地址包含一个或两个"."符号。
输入中任意一个域名的访问次数都小于10000。


A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.

Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".

We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

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.

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.

Notes:

The length of cpdomains will not exceed 100. 
The length of each domain name will not exceed 100.
Each address will have either 1 or 2 "." characters.
The input count in any count-paired domain will not exceed 10000.
The answer output can be returned in any order.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3814 访问。

public class Program {public static void Main(string[] args) {var cpdomains = new string[] { "9001 discuss.leetcode.com" };var res = SubdomainVisits(cpdomains);ShowArray(res);Console.ReadKey();}private static void ShowArray(IList<string> array) {foreach(var domain in array) {Console.Write($"{domain} ");}Console.WriteLine();}private static IList<string> SubdomainVisits(string[] cpdomains) {var dic = new Dictionary<string, int>();foreach(var domain in cpdomains) {var split = domain.Split(' ');var num = int.Parse(split[0]);var subDomains = GetSubDomains2(split[1]);foreach(var item in subDomains) {if(dic.ContainsKey(item)) {dic[item] += num;} else {dic[item] = num;}}}return dic.Select(r => r.Value + " " + r.Key).ToList();}private static List<string> GetSubDomains(string domain) {var res = new List<string>();var domains = domain.Split('.');for(var i = 0; i < domains.Length; i++) {var dom = string.Empty;for(var j = i; j < domains.Length; j++) {dom += "." + domains[j];}res.Add(dom.TrimStart('.'));}return res;}private static List<string> GetSubDomains2(string domain) {var res = new List<string>();for(var i = domain.Length - 1; i >= 0; i--) {if(domain[i] == '.' || i == 0) {res.Add(domain.Substring(i).TrimStart('.'));}}return res;}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3814 访问。

9001 discuss.leetcode.com 9001 leetcode.com 9001 com

分析:

设原问题规模为 n,域名的最大长度为 m,若获取子域名采用 GetSubDomains 方法,那么以上算法的时间复杂度为:  ;

若获取子域名采用 GetSubDomains2 方法,则以上算法的时间复杂度为:  。

C#LeetCode刷题之#811-子域名访问计数​​​​​​​(Subdomain Visit Count)相关推荐

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

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. LeetCode每日一题——811. 子域名访问计数

    LeetCode每日一题系列 题目:811. 子域名访问计数 难度:普通 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 网站域名 "discuss.leetcode. ...

  3. LeetCode·每日一题·811.子域名访问计数·哈希

    链接:https://leetcode.cn/problems/subdomain-visit-count/solution/-by-xun-ge-v-i3sb/ 来源:力扣(LeetCode) 著作 ...

  4. LeetCode 811. 子域名访问计数

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

  5. LeetCode Algorithm 811. 子域名访问计数

    Ideas 计数配对域名是由域名访问次数和域名组成的,那么对应域名的每一级域名都访问了相应次. 那么我们可以遍历计数配对域名组成的数组,对于每个计数配对域名,可以先把域名按照.分隔开,然后由一个总的计 ...

  6. LeetCode(811)——子域名访问计数(JavaScript)

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

  7. 每天Leetcode 刷题 初级算法篇-数学问题-计数质数

    题目要求: 力扣题解: 代码 import java.util.Arrays;/*** @program: mydemo* @description: 力扣-数学-计数质数* @author: Mr. ...

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

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

  9. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  10. 【LeetCode刷题】二月汇总篇

    学习总结 文章目录 学习总结 一.时间安排 task01 数组 task02 链表 task03 栈 task04 字符串 task05 树 task06 位运算 task07 双指针 task08 ...

最新文章

  1. python语言怎么学-如何从零开始学习Python,python语言编程入门
  2. 《地狱之刃:塞娜的献祭》如何通过人物情感营造恐怖氛围?
  3. asp.net 的page 基类页面 做一些判断 可以定义一个基类页面 继承Page类 然后重写OnPreLoad事件...
  4. 为Envoy构建控制平面的指南-特定于域的配置API
  5. hdu 1317——XYZZY
  6. JS 设计模式 一(接口)
  7. Java 8 为什么要使用Lambda表达式
  8. 在 Mac 上如何使用鼠标键来控制指针?
  9. LINUX检查一个进程内存增长的脚本
  10. 信号量内核对象 semaphore
  11. 用于登录的mysql语句_mysql常用语句
  12. RFC 文档(1001-1500)
  13. 本周AI热点回顾:Hinton独立发布44页论文火爆社区;新特效火爆全网!各路神仙齐唱《蚂蚁呀嘿》...
  14. 联想台式计算机亮度怎么调,台式联想电脑亮度在哪里调(手把手教你调电脑亮度)...
  15. Excel2010 柱形图与折线图制表
  16. 20年java面试_Java 20年
  17. 18 What is the __dict__.__dict__attribute of a Python class
  18. 2020南京理工大学计算机考研经验
  19. 2016服务器系统如何查询真伪,数据查询的方法、客户端、服务器及系统
  20. 企业微信私域流量运营应当掌握的“工具”

热门文章

  1. LeetCode 83. Remove Duplicates from Sorted List
  2. Julia常用包总结(深度学习、数据科学、绘图...updating...)
  3. 0514实训演练 新建项目 使用java编写类与对象 入门
  4. sqlserver 日期函数
  5. java游戏循环 限定次数的游戏
  6. linux-索引1909
  7. linux-facl权限入门-设置与查看facl权限
  8. git-索引-1909
  9. javascript-变量的作用域
  10. Laravel核心技术解析(1)—— Composer 组件管理与自动加载