题目要求

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.

题目分析及思路

互联网在访问某个域名的时候也会访问其上层域名,题目给出访问该域名的次数,统计所有域名被访问的次数。可以使用字典统计次数,collections.defaultdict可自行赋值。上层域名采用while循环和字符串切片来访问。

python代码

class Solution:

def subdomainVisits(self, cpdomains: 'List[str]') -> 'List[str]':

domain_counts = collections.defaultdict(int)

for cpdomain in cpdomains:

count, domain = cpdomain.split()

count = int(count)

domain_counts[domain] += count

while '.' in domain:

domain = domain[domain.index('.')+1 :]

domain_counts[domain] += count

return [str(v) + ' ' + k for k, v in domain_counts.items()]

LeetCode 811 Subdomain Visit Count 解题报告的更多相关文章

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

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

  2. LeetCode 811. Subdomain Visit Count (子域名访问计数)

    题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...

  3. 811. Subdomain Visit Count - LeetCode

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

  4. 【Leetcode_easy】811. Subdomain Visit Count

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

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

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

  6. 811. Subdomain Visit Count (5月23日)

    解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...

  7. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】498. Diagonal Traverse 解题报告(Python)

    [LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...

随机推荐

  1. 转:pycharm community debug django projects

    原文:https://automationpanda.com/2017/09/14/django-projects-in-pycharm-community-edition/comment-page- ...

  2. Android开发(十六)——Android listview onItemClick事件失效的原因

    参考: Android listview onItemClick事件失效的原因.http://blog.csdn.net/wangchun8926/article/details/8793178

  3. JVM——Java HotSpot VM Options

    JVM常用参数 参数名称 含义 默认值  描述 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆 ...

  4. 稍稍解读下ThreadPoolExecutor

    # 说说ThreadPoolExecutor ## 认识 先来看看它所在的架构体系: ```java package java.util.concurrent; public interface Ex ...

  5. asp.net Identity 设置自定义登录

    添加Startup.Auth.cs public partial class Startup { // For more information on configuring authenticati ...

  6. 《objective-c基础教程》学习笔记(四)—— OC面向对象编程初探

    在上篇博文中,我们编写了一个可以输出不同几何类型的小程序.通过C语言的struct结构体,给大家感受了下,对象的大概样子. 如果用Obejctive-C的面向对象的特征来实现.那么,drawShape ...

  7. 【CF587D】Duff in Mafia 二分+前缀优化建图+2-SAT

    [CF587D]Duff in Mafia 题意:给你一张n个点m条边的无向图,边有颜色和边权.你要从中删去一些边,满足: 1.任意两条删掉的边没有公共的顶点.2.任意两条剩余的.颜色相同的边没有公共 ...

  8. JSP页面嵌套乱码解决

    项目中审批过程需要将业务表单嵌套在审批的页面中.由于业务表单很多,前台已经axjx到了本次选择的表单的地址.本来做的就是把这个链接放在审批页面上,但现在需求的就是直接把这个biz表单嵌套在审批的页面中 ...

  9. I - Cows

    来源 poj 3348 Your friend to the south is interested in building fences and turning plowshares into sw ...

  10. 微信小程序:bindtap等事件传参

    事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数. 事件对象可以携带额外信息,如 id, data ...