【算法题解】 Day7 偷懒的一天

简介: I'm so sorry, 今天出去玩了,所以只有每日一题的签到,偷懒了偷懒了,下次不会了,sos...

前言

I'm so sorry, 今天出去玩了,所以只有每日一题的签到...

每日一题

811. 子域名访问计数 难度:medium

题目

网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为"discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "leetcode.com" 以及 "com" 。

计数配对域名 是遵循 "rep d1.d2.d3" 或 "rep d1.d2" 格式的一个域名表示,其中 rep 表示访问域名的次数,d1.d2.d3 为域名本身。

  • 例如,"9001 discuss.leetcode.com" 就是一个 计数配对域名 ,表示 discuss.leetcode.com 被访问了 9001 次。

给你一个 计数配对域名 组成的数组 cpdomains ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。

示例 1:

输入: cpdomains = ["9001 discuss.leetcode.com"]
输出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
解释:例子中仅包含一个网站域名:"discuss.leetcode.com"。
按照前文描述,子域名 "leetcode.com" 和 "com" 都会被访问,所以它们都被访问了 9001 次。

示例 2:

输入:cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出:["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
解释:按照前文描述,会访问 "google.mail.com" 900 次,"yahoo.com" 50 次,"intel.mail.com" 1 次,"wiki.org" 5 次。
而对于父域名,会访问 "mail.com" 900 + 1 = 901 次,"com" 900 + 50 + 1 = 951 次,和 "org" 5 次。

 

方法一:哈希表

思路

每个计数配对域名的格式都是 "rep d1.d2.d3" 或 "rep d1.d2"。子域名的计数如下:

  • 对于格式 "rep d1.d2.d3",有三个子域名 "d1.d2.d3"、"d2.d3" 和 "d3",每个子域名各被访问 rep 次;
  • 对于格式 "rep d1.d2","d1.d2" "d2",每个子域名各被访问 rep 次。

为了获得每个子域名的计数配对域名,需要使用哈希表记录每个子域名的计数。遍历数组 cpdomains,对于每个计数配对域名,获得计数和完整域名,更新哈希表中的每个子域名的访问次数。

遍历数组 cpdomains 之后,遍历哈希表,对于哈希表中的每个键值对,关键字是子域名,值是计数,将计数和子域名拼接得到计数配对域名,添加到答案中。

 

解题

Python:

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        cnt = Counter()
        for domain in cpdomains:
            c, s = domain.split()
            c = int(c)
            cnt[s] += c
            while '.' in s:
                s = s[s.index('.') + 1:]
                cnt[s] += c
        return [f"{c} {s}" for s, c in cnt.items()]

Java:

class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        List<String> ans = new ArrayList<String>();
        Map<String, Integer> counts = new HashMap<String, Integer>();
        for (String cpdomain : cpdomains) {
            int space = cpdomain.indexOf(' ');
            int count = Integer.parseInt(cpdomain.substring(0, space));
            String domain = cpdomain.substring(space + 1);
            counts.put(domain, counts.getOrDefault(domain, 0) + count);
            for (int i = 0; i < domain.length(); i++) {
                if (domain.charAt(i) == '.') {
                    String subdomain = domain.substring(i + 1);
                    counts.put(subdomain, counts.getOrDefault(subdomain, 0) + count);
                }
            }
        }
        for (Map.Entry<String, Integer> entry : counts.entrySet()) {
            String subdomain = entry.getKey();
            int count = entry.getValue();
            ans.add(count + " " + subdomain);
        }
        return ans;
    }
}

 

后记

📝 上篇精讲: 【算法题解】 Day6 BFS | DFS
💖 我是  𝓼𝓲𝓭𝓲𝓸𝓽,期待你的关注;
👍 创作不易,请多多支持;
🔥 系列专栏: 算法题解
相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
7月前
|
算法
代码随想录 Day26贪心算法01-上
代码随想录 Day26贪心算法01-上
28 1
|
6月前
LeetCode刷题之 存在重复元素(题目分析➕源代码)
LeetCode刷题之 存在重复元素(题目分析➕源代码)
|
9月前
|
算法
代码随想录 Day36 - 贪心算法(五)
代码随想录 Day36 - 贪心算法(五)
33 0
|
9月前
|
算法
代码随想录 Day35 - 贪心算法(四)
代码随想录 Day35 - 贪心算法(四)
27 0
|
9月前
|
监控 算法
代码随想录 Day37 - 贪心算法(六)
代码随想录 Day37 - 贪心算法(六)
33 0
|
9月前
|
算法
代码随想录 Day34 - 贪心算法(三)
代码随想录 Day34 - 贪心算法(三)
42 0
|
9月前
|
算法
代码随想录 Day31 - 贪心算法(一)
代码随想录 Day31 - 贪心算法(一)
30 0
|
9月前
|
算法
代码随想录 Day32 - 贪心算法(二)
代码随想录 Day32 - 贪心算法(二)
35 0
|
12月前
|
算法 C++
【每日算法Day 103】老题新做,几乎不会有人想到的解法,它来了
【每日算法Day 103】老题新做,几乎不会有人想到的解法,它来了
|
C语言
【leetcode】学了栈和队列却觉得无用武之地?试试这几道题目吧!
【leetcode】学了栈和队列却觉得无用武之地?试试这几道题目吧!
72 0