[Leetcode] 811. Subdomain Visit Count 解题报告

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 阅读详情

题目:

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 exceed 100. 
  • 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.

思路:

一道简单的字符串处理和哈希表应用的题目:我们首先定义一个哈希表,记录从各个subDomain到其出现次数的映射。然后对于cpdomains中的每个元素,分别解析出该domain出现的次数,以及它所有的superDomains(我们定义discuss.leetcode.com的superDomains为leetcode.com,以及.com),然后将它本身以及所有对应的superDomains的访问次数都更新。最后返回符合格式的哈希表中的内容即可。

代码:

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        for (auto &s : cpdomains) {
            int index = s.find_first_of(' ');
            int count = stoi(s.substr(0, index));
            string domain = s.substr(index + 1);
            getSubdomainVisits(domain, count);
        }
        vector<string> ret;
        for (auto it = hash.begin(); it != hash.end(); ++it) {
            ret.push_back(to_string(it->second) + " " + it->first);
        }
        return ret;
    }
private:
    void getSubdomainVisits(const string &s, int count) {
        int index = 0;
        do {
            string subdomain = index == 0 ? s.substr(index) : s.substr(index + 1);
            hash[subdomain] += count;
            index = s.find_first_of('.', index + 1);
        } while (index != string::npos);
    }
    unordered_map<string, int> hash;
};
10种电阻综合对比——《器件手册--电阻》 可变电阻在电子设备中也有很多用途,如在收音机的音量调节电路中,通过旋转可变电阻的旋钮来改变电路中的电阻值,从而改变通过扬声器的电流,实现音量的调节。根据电阻定律,电阻R = ρ(L/S),其中ρ是导体材料的电阻率,L是导体的长度,S是导体的横截面积。比如,同样材料制成的两根电线,粗的电线电阻比细的电线电阻小,长的电线电阻比短的电线电阻大。电流采样电阻的工作原理基于欧姆定律,即电阻两端的电压与通过电阻的电流成正比,公式为 I = V/R,其中 I是电流,V是电阻两端的电压,R 是电阻值。 阅读详情

相关推荐

鸿蒙读书笔记1:《鸿蒙操作系统设计原理与架构》

GUI、 语 音 控 制 方 式 的 VUI(Voice User Interface, 语 音 用 户 界。电话、多媒体、DFX、MSDP(Multimodal Sensor Data Platform,多。面)、手势/姿态控制的CVUI(Computer Vision User Interface,计算。KAL(Kernel Abstract Layer,内核抽象层)通过屏蔽多内核差异,JavaScript、C、C++等多语言的用户程序开发框架,以及UI框架、

jackyrongvip的专栏 2452

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"...

leetcode 解题思路 247

手把手教你用Qt和DSP28335实现串口Bootloader,附赠源码和避坑指南

本文详细介绍了如何使用Qt和DSP28335实现串口Bootloader,包括开发环境搭建、通信协议设计、Qt上位机开发、DSP端Bootloader核心实现以及应用程序工程的特殊配置。通过实战案例和源码分享,帮助开发者快速掌握嵌入式固件升级技术,提升工业设备远程升级的效率和可靠性。

weixin_29176179的博客 137

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.co...

博客 453

计算机组成原理(二)—— 数据的表示与运算

本章目录引言一、数制与编码1.1 进位计数制1.1.1 进制及转换1.1.2 真值、机器数1.2 BCD码1.3 ASCII 码1.4 校验码1.4.1 奇偶校验码1.4.2 海明码1.4.3 CRC循环冗余码二、定点数的表示和运算2.1 定点数的表示2.1.1 无符号2.1.2 有符号(原码、反码、补码、移码)2.2 定点数的运算2.2.1 移位运算2.2.2 加减运算(溢出判断)2.2.3 乘法运算2.2.4 除法运算2.3 强制类型转换2.4 数据的存储和排列三、浮点数的表示和运算3.1 浮点数的表示

S mile0804的博客 7479

谷歌倾斜摄影数据获取以及转换3DTiles介绍

解析谷歌倾斜模型数据,再一键设置无人值守的情况下,快速将谷歌倾斜摄影模型数据转成3DTiles数据,并在Cesium上进行流畅漫游缩放显示,效果优于官方3DTiiles格式数据

cyber_room的博客 5339

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.leetcod...

洞玄之境的博客 364

数据库课程案例——电力公司收费管理系统分析与实践,完整带SQL语句

某电力公司收费管理信息系统实现客户信息、用电类型(类别号、类别名、电价)及业务员管理;实现客户用电信息管理(客户号、月份、用电类别号、用电度数);实现客户费用管理(客户号、月份、费用、收费标志),收费标志的默认值为“未收”;实现收费登记(客户、月份、应收费用、实收费用、业务员),并自动修改收费标志(用触发器实现);创建触发器,实现收费时自动更加应收费用和实收费用,计算本次结余,然后修改客户信息表中的结余金额;创建存储过程统计指定月份应收费用和实收费用;创建存储过程查询指定月份未交费的用户信息,以便崔费;创建

qq_44242804的博客 2765

子域名访问计数 Subdomain Visit Count

一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。 给定一个带访问次数和

make_a_great_effort的博客 173

Vue3 - [兼容PC和手机H5] 详细监听浏览器刷新 / 关闭 / 前进后退事件,用户点击关闭和刷新页面前 “拦截“ 操作并弹出提示框(实时监听用户关闭或刷新网页,触发时文字提醒并执行自定义操作)

vue3监听页面关闭和刷新,vue3中如何监听页面刷新和关闭事件,vue3中监听浏览器窗口关闭事件,并在窗口关闭进行操作,vue3怎么只在某个页面监听,其他页面不监听,vue3监听刷新和关闭怎么离开页面时注销释放,vue3怎么阻止浏览器前进和后退并提示,页面跳转离开后依旧可以监听到刷新和关闭,vue3怎么知道用户关闭了网页,vue3如何监听刷新网页操作并截断,vue3移动端手机h5网页手机网站监听浏览器刷新关闭,vue3当页面关闭或刷新时怎么提示用户离开不保存数据,vue3项目监听浏览器窗口或标签页关闭,并

🏆 前端领域优质创作者 1万+

Python爬虫实战:逆向工程与异步爬虫技术,高效抓取国家知识产权局专利数据

相比于传统的Selenium,Playwright由微软开发,支持多浏览器(Chromium, Firefox, WebKit),API更现代,执行速度更快,并且能轻松录制用户操作生成代码。通常,列表中的摘要信息是缩略的,我们需要进入每个专利的详情页获取完整信息(如主权要求、法律状态等)。我们的第一步是手动操作一遍搜索流程,并用Playwright的“录制”功能或开发者工具,找出真正返回专利数据的那一个或多个网络请求。以友好的、对人类行为模拟的方式爬取数据是基本的网络礼仪,也能有效避免你的IP被封锁。

2201_76125261的博客 2424

【leetcode 811】Subdomain Visit Count

##Map#####[Subdomain Visit Count]#######811-问题描述####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.c...

drunkpragrammer的博客 807

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(一)

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(一)

宁波阿成的博客 665

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.leetcod...

weixin_30436101的博客 106

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【LeetCode】811. Subdomain Visit Count 解题报告(Python)

【LeetCode】811. Subdomain Visit Count 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/subdomain-visit-count/description/ 题目描述: A website domain like “discuss.leetcode.com” co...

负雪明烛 3583

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode Hash Table知识点总结

454. 4Sum II:求满足A[i]+B[j]+C[k]+D[l] = 0的 (i,j,k,l)个数,Medium http://bookshadow.com/weblog/2016/11/13/leetcode-4sum-ii/ 利用字典cnt,将A,B中各元素(笛卡尔积)的和进行分类计数。 将C,D中各元素(笛卡尔积)和的相反数在cnt中的值进行累加,即为答案。 ...

大锅八十小锅四十 412

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode //C - 811. Subdomain Visit Count

Algorithm Summary: The solution counts subdomain visits by parsing each input string into a visit count and domain, then iteratively processes all possible subdomains (e.g., "discuss.leetcode.com" → "leetcode.com" → "com"). A

Made in Code 1555

Leetcode PHP题解--D36 811. Subdomain Visit Count

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33849215的博客 121

811. Subdomain Visit Count*

811. Subdomain Visit Count* https://leetcode.com/problems/subdomain-visit-count/ 题目描述 A website domain like “discuss.leetcode.com” consists of various subdomains. At the top level, we have “com”, at t...

珍妮的选择的博客 1452

[leetcode]811. Subdomain Visit Count

[leetcode]811. Subdomain Visit Count Analysis old friends~—— [嘻嘻~] A website domain like “discuss.leetcode.com” consists of various subdomains. At the top level, we have “com”, at the next l...

weixin_32135877的博客 125

Leetcode——811. Subdomain Visit Count

题目原址 https://leetcode.com/problems/subdomain-visit-count/description/ 题目描述 A website domain like “discuss.leetcode.com” consists of various subdomains. At the top level, we have “com”, at the nex...

想当厨子的程序媛 321

[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.c...

jing16337305的博客 423

Leetcode 811. Subdomain Visit Count

class Solution(object): def subdomainVisits(self, cpdomains): """ :type cpdomains: List[str] :rtype: List[str] """ count = {} for cpdom in ...

weixin_30834019的博客 96

Leetcode 811.子域名访问计数(Subdomain Visit Count)

Leetcode 811.子域名访问计数 1 题目描述(Leetcode题目链接)   一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 “com”。 给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为

qq_39378221的博客 343

D36 811. Subdomain Visit Count

811. Subdomain Visit Count 题目链接 811. Subdomain Visit Count 题目分析 题目给定一个字符串数组,每个字符串分两部分,以空格分割。 第一部分为访问次数,第二部分为域名。 要求按同样的格式,分别返回顶级域名、二级域名、三级域名…的访问次数。 例如,字符串"9001 discuss.leetcode.com"。 discuss.leetcode.c...

weixin_34390105的博客 177

Leetcode 811. Subdomain Visit Count(Java)

问题描述 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.leetcod...

lovrain的博客 807
上一篇: [Leetcode] 810. Chalkboard XOR Game 解题报告
下一篇: [Leetcode] 812. Largest Triangle Area 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值