1、题目描述

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.

原题: https://leetcode.com/problems/subdomain-visit-count/description/

题目的意思是输入一个字符串向量,每个字符串代表一个域名和访问这个域名的次数,统计将域名拆分之后。所有的的域名的访问次数。

2、题目分析

由于输入的每个字符串都是string,因此应该考虑先将字符串拆分,然后使用一个unorder_map<string,int>统计每个子域名出现的次数。

3、代码

 vector<string> subdomainVisits(vector<string>& cpdomains) {
// 思路可以将字符串先分割,然后使用 unorder_set 计数
// 使用一个子函数将每个cpdomains 分割 unordered_map<string,int> m; for(auto Itr = cpdomains.begin() ; Itr != cpdomains.end(); Itr++ )
{
vector<string> Sproc = split_cpdomains(*Itr);
for(auto itr = Sproc.begin() + ; itr != Sproc.end(); itr++ )
{
m[*itr] += stoi(Sproc[]);
}
} vector<string> ans;
for(auto it = m.begin() ; it != m.end(); it++ )
{
ans.push_back( to_string(it->second) +" "+ it->first );
} return ans; } vector<string> split_cpdomains(string & s)
{
vector<string> res; int i = ;
for(auto itr = s.begin() ; itr != s.end() ; itr++) // 以下代码可以用 string 的find() 成员函数简化
{
i++;
if( *itr == ' ')
{
res.push_back( s.substr(,i )); // put number in
break;
}
} res.push_back( s.substr( s.find_first_of(' ') + )) ;
res.push_back( s.substr(s.find_first_of(".") + ));
if( s.find_first_of(".") != s.find_last_of(".") )
res.push_back( s.substr( s.find_last_of(".") + ) ) ; return res;
}

leetcode题解之分解字符串域名的更多相关文章

  1. leetCode题解之反转字符串中的元音字母

    1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...

  2. 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)

    目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...

  3. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  4. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  5. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  6. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  7. leetcode题解(持续更新)

    leetcode题解 1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  8. LeetCode题解汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...

  9. LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...

随机推荐

  1. 如何使用Android Studio提高App质量

    Android Studio作为现在谷歌主推的Android开发功能,除了提供了大量的功能帮助快速开发Android代码之外,在代码质量控制方面也提供了很多工具,这些工具都放在Analyze菜单下, ...

  2. C/C++ -- Gui编程 -- Qt库的使用 -- 组件大杂烩

  3. vue+Element-ui实现分页效果

    当我们向后台请求大量数据的时候,并要在页面展示出来,请求的数据可能上百条数据或者更多的时候,并不想在一个页面展示,这就需要使用分页功能来去完成了. 1.本次所使用的是vue2.0+element-ui ...

  4. tomcat启动(三)Catalina分析-load方法分析

    load()方法按从上到下顺序分析(主要分析本人所没学过的知识点,其它略过...). Digester类作用 使用sax技术对xml进行解析 未开始解析时Digester.push(this)这个用来 ...

  5. Dock的生态开源技术(Etcd&Machine&Compose&Swarm&Mesos&Kubernetes)

    Etcd CoreOS公司开源的高可用分布式键值数据库Etcd,该项目已经被广泛应用到分布式系统的一致性实现和服务发现中,基于Go语言实现. Etcd就是专门为集群环境设计,可以很好地实现数据一致性, ...

  6. C#的SubString(int start,int end);

    C#的SubString(); 例子: string str = "i am a girl"; string temp = str.Substring(2,2);//从索引2开始, ...

  7. Innosetup(pascal)标签控件label换行

    Label1.AutoSize := false; //先关闭自适应 Label1.WordWrap := true; //开启换行

  8. 笔记五:python字符串

    一:学习内容 字符串类型 字符串类型判断 字符串类型互转 字符串小练习 二:字符串类型 1. basestring 在python中和字符串相关的数据类型为:str和unicode,他们都是bases ...

  9. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  10. 自动化部署MySQL 5.6 步骤 制作到ftp共享,永远使用

    首先需要搭建ftpserver yum install vsftpd service vsftpd start 这样ftp服务就起来了,这里只是简单的使用,所以没有使用配置文件.这样我们只要将需要的文 ...