LeetCode 811 Subdomain Visit Count 解题报告
题目要求
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 解题报告的更多相关文章
- 【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 - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- [LeetCode&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- 811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】498. Diagonal Traverse 解题报告(Python)
[LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...
随机推荐
- Smack类库详细介绍
原文地址:http://blog.csdn.net/xunshu/archive/2008/03/27/2223817.aspx Smack是一个为使用XMPP服务器聊天和发送即时消息交流而提供的库. ...
- php中static和self的区别
在阅读一些框架的源码时发现了new static(),和new self(),甚是不解,后来查阅资料,才知道static采用了延迟绑定,能准确知道是父类还是子类的调用.这就是说static是个聪明的小 ...
- 要是VISUAL STUDIO 2015带这些功能就好了
visual studio 2015 正式版立即就要出来了,事实上我原来满期待微软能出一套完美的移植的ANDROID和IOS应用的技术方案,这样WIN10正式版出来后,有一套比較好的移植框架,大家能够 ...
- Tomcat 全攻略
转自:http://www.ibm.com/developerworks/cn/java/l-tomcat/ 简介 tomcat 是 jakarta 项目中的一个重要的子项目,其被 JavaWorld ...
- A Tour of ParallelExtensionsExtras
Throughout the development of Parallel Extensions for the .NET Framework 4, we've come across a myri ...
- Scala学习笔记(二):object、伴生对象和基本类
object object 是只有一个实例的类.它的定义与Java中的class类似,如: // 单例对象 object AppEntry { def main(args: Array[String] ...
- 在Android Studio中查看Sqlite的方法
只说最好的方法,使用工具stetho:http://facebook.github.io/stetho/ 1.在Gragle中加上如下语句: dependencies { // Stetho core ...
- C语言的标准库和系统调用
http://blog.csdn.net/yusiguyuan/article/details/23181327 Linux系统调用这部分经常出现两个词:libc库和封装函数,不知道你是否清楚它们的含 ...
- 【Docker】基本命令
1.进入容器交互状态 docker exec -ti xxxx /bin/bash 2.查询镜像 docker images 3.查询容器 docker ps
- 系统信号(signal)与其他(定时器,退出清理等)
信号signal,可以用作进程线程通信,也可以用作接收中断后退出,退出时,清理资源,记录日志.python相关包为signa. linux信号表 root@server:~# kill -l ) SI ...