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. artTemplate-优秀的前端模板引擎

    artTemplate-优秀的前端模板引擎 1.html中引入artTemplate的js文件: <script type="text/javascript" src=&qu ...

  2. 《Algorithms算法》笔记:优先队列(1)——API和初等实现

    1.优先队列的API和初等实现 做一个总结: 栈 :先进后出 队列 :先进先出 随机队列 : 随机出 优先队列:每次出来的是最大值或最小值 1.1优先队列的API 优先队列在很多场合都有用, 比如:在 ...

  3. Android 开发工具类 26_getFile

    从网络获取可执行文件 public void getFile() throws Exception{ // 首先得到请求的路径 String urlpath = "http://ftpcnc ...

  4. Notepad++软件的下载与安装步骤(图文详解)

    不多说,直接上干货! 这款软件非常好用!!!   1.下载Notepad++软件 Notepad++非常好用,想要安装首先我们要下载(废话)~ 百度搜索“Notepad++”直接就可以找到主页: 主页 ...

  5. Java虚拟机(三):JVM垃圾回收机制

    概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本地方 ...

  6. 07-python链接mysql

    python3 中使用的是 PyMySQL模块, 取代了之前的 MysqlDB 模块, 其实使用都是一样的, 如果没有该模块的, 需要使用pip命令进行安装 pip install PyMySQL 安 ...

  7. 剑指offer64:滑动窗口的最大值

    题目描述: 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4, ...

  8. Node.js之HTPP URL

    几乎每门编程语言都会包括网络这块,Node.js也不例外.今天主要是熟悉下Node.js中HTTP服务.其实HTTP模块是相当低层次的,它不提供路由.cookie.缓存等,像Web开发中不会直接使用, ...

  9. PHP 集成开发环境比较

    专注了这么些年技术,没有养成记录和积累的习惯.如今乐于开源和分享经验,却停笔踌躇,不知该从何处说起.开通博客也有一段时间了,也没能写出一篇像样的文章,其实这篇文章也是被我拉壮丁似的用来练手的.思前想后 ...

  10. [硬件知识]OP(Over-provisioning)预留空间

    SSD上的OP指的是用户不可操作的容量,大小为实际容量减去用户可用容量,OP区域一般被用于优化操作如:WL,GC和坏块映射等. OP一般分三层(见下图).第一层容量固定为SSD标称容量的7.37%,这 ...