[LeetCode&Python] Problem 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.
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.
Notes:
- The length of
cpdomains
will not exceed100
. - 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.
Solution:
class Solution:
def subdomainVisits(self, cpdomains):
"""
:type cpdomains: List[str]
:rtype: List[str]
"""
counter=collections.Counter()
for cp in cpdomains:
count, *d=cp.replace(" ",".").split(".")
"""
Explanation:
a="Hello World.Haha"
a.replace(" ",".")
-> "Hello.World.Haha"
a.replace(" ",".").split(".")
-> ["Hello","World","Haha"]
count, *d=a.replace(" ",".").split(".")
-> count="Hello", d=["World","Haha"]
"""
for i in range(len(d)):
counter[".".join(d[i:])]+=int(count)
return [" ".join((str(c),s)) for s,c in counter.items()]
""" Explanation:
Collections.Counter() is a container to store elements as
dictionary keys and the number of elements as dictonary value. a=[1,2,1,2,1]
Counter(a)
-> Counter({1: 3, 2: 2})
Counter(a).items()
-> dict_items([(1, 3), (2, 2)])
Counter(a).values()
-> dict_values([3, 2])
Counter(a).keys()
-> dict_keys([1, 2])
"""
[LeetCode&Python] Problem 811. Subdomain Visit Count的更多相关文章
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- 【LeetCode】811. Subdomain Visit Count 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- 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 ...
随机推荐
- Xcode集成POD教程
http://www.cocoachina.com/ios/20150410/11526.html COCOAPODS的网站上有很多非常好用的资源,这里来说一下如何把POD集成到我们的Xcode项目中 ...
- xinwenti
angularjs angular2脏检查机制和数据双向绑定远离 angular2 aot编译
- Java 常用对象-System类
2017-11-02 21:41:06 System类:System 类包含一些有用的类字段和方法.它不能被实例化. *常用方法 public static void gc() 运行垃圾回收器. 调用 ...
- ArcGIS API for Silverlight——小滑块
Widgets翻译过来是小玩具.如果使用过Dojo或者ExtJS等js框架肯定会了解到这个“小玩具”也有大用处,能够在很大程度上减少我们的工作量,快速完成功能需求.能减少多大工作量呢?让我们先来,点击 ...
- codeforces 484a//Bits// Codeforces Round #276(Div. 1)
题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...
- android--------阿里 Sophix移动热修复
移动热修复(Mobile Hotfix)是阿里云提供的全平台App热修复服务方案.产品基于阿里巴巴首创hotpatch技术,提供最细粒度热修复能力,让您无需等待实时修复应用线上问题. 移动热修复提供的 ...
- UVA-10273 Cyborg Genes (DP)
题目大意:给两个字符串a.b,找出一个最短的字符串c,使得这两个字符串都是c的子序列.只需找出p的最小长度和最小长度时的个数. 题目分析:与LCS问题类似.最小长度的状态转移方程,dp(i,j)=mi ...
- ORA-14452:试图创建,更改或删除正在使用的临时表中的索引
因为表kol_xx_fin050_temp 为临时表,而且有其他session正在使用. select vs.* from v$session vs , v$lock vl , dba_objects ...
- java并发编程:线程安全管理类--原子操作类--AtomicStampedReference<V>
1.类 AtomicStampedReference<V> AtomicStampedReference 维护带有整数“标志”的对象引用,可以用原子方式对其进行更新. 实现注意事项.通过创 ...
- Microsoft Word 2007 向程序发送命令时出现问题解决方法
最近在打开Word文档时总是出现“向程序发送命令时出现问题”对话框,而且不确定性,关闭重新打开有时没事了有时还不行, 很让人头疼,经过尝试,把问题解决了 1.问题截图如下: 2.解决方法 1)方法一: ...