题目要求

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. Python(十)之GUI编程

    在windwos上实现:使用wxPython模块 #!/usr/bin/env python # encoding: utf-8 import wx #创建open按钮触发的事件对应的函数 def l ...

  2. 【iCore1S 双核心板_FPGA】例程五:Signal Tapll 实验——逻辑分析仪

    核心代码: //--------------------Module_Signal_TapII-------------------// module Signal_TapII( input CLK_ ...

  3. Oracle 11g EM删除重建的方法

    虚拟机里的Oracle 11g好长时间没用了,突然打开之后发现EM无法访问了,EM可以重建,于是也不打算查找原因了,直接使大招 OS:Windows Server 2012 Oracle:11g R2 ...

  4. 使用Sublime Text搭建python调试环境[转]

    pycharmt等IDE虽然用着爽,但毕竟在速度.资源上还是比较让人不爽的. 使用IDE无非是图个方便省事,特别是像我这种有些记性差的来说. IDE说起来方便于的几个地方就是: 1.语法颜色高亮 2. ...

  5. [Full-stack] 世上最好语言 - PHP

    前言 本篇是对个人PHP, Laravel系列博文的总结与思考. 目的在于理清并熟练如下过程: "需求 --> Usercase --> UI --> 框架 --> ...

  6. 如何处理MySQL每月5亿的数据

    第一阶段:1,一定要正确设计索引2,一定要避免SQL语句全表扫描,所以SQL一定要走索引(如:一切的 > < != 等等之类的写法都会导致全表扫描)3,一定要避免 limit 100000 ...

  7. C#基础系列——语法

    1.C#是这样开始的: 函数入口:static void  Main(String [] args){} 2.Hello  World 例子 using system;--------导入命名空间,里 ...

  8. mysql无法生成log文件

    做服务器主从配置时,发现/var/log/mysql下面一直没有log文件,明明配置文件/etc/mysql/mysql.conf.d/mysqld.cnf也开启log了. general_log_f ...

  9. js中的XMLHTTPRequest

    window.onload = function(){ //var url = "http://localhost:8000/sales.json"; var url = &quo ...

  10. 【转载】Eclipse 的快捷键以及文档注释、多行注释的快捷键

    一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释 3.以上快捷 ...