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()]

原文地址:https://www.cnblogs.com/yao1996/p/10355636.html

时间: 2024-11-05 23:23:27

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

811. Subdomain Visit Count - LeetCode

Question 811.?Subdomain Visit Count 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&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

白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊.今日重新开始吧,先来一道简单的题目,如下: 这道题首先想到的还是使用Map,代码如下: /** * @param {string[]} cpdomains * @return {string[]} */ var subdomainVisits = function(cpdomains) { let tmp = new Map(); let res = new Array(); for(let i = 0 ; i < cpdomains

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

811. Subdomain Visit Count (5月23日)

解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains) { vector<string> result; map<string,int> pair; for(string str:cpdomains){ auto space=str.find(' '); int temp=stoi(str.substr(0,space)); str=s

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of