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. 两个字符串 char* a, char* b,输出b在a中的位置次序。

    /** 题目: 两个字符串 char* a, char* b,输出b在a中的位置次序. void output_postion(const char* a, const char* b); 如:a = ...

  2. Go语言学习笔记五: 条件语句

    Go语言学习笔记五: 条件语句 if语句 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 竟然没有括号,和python很像.但是有大括号,与python又不一样. 例子: pa ...

  3. 用python写一个爬虫——爬取性感小姐姐

    忍着鼻血写代码 今天写一个简单的网上爬虫,爬取一个叫妹子图的网站里面所有妹子的图片. 然后试着先爬取了三页,大概有七百多张图片吧!各个诱人的很,有兴趣的同学可以一起来爬一下,大佬级程序员勿喷,简单爬虫 ...

  4. C#读取excel文件的内容(使用DataSet)

    C#读取Excel文件的内容,通过OLEDB来连接,关键是连接的路径,如:string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data S ...

  5. 我的zsh 配置

    # If you come from bash you might have to change your $PATH.# ZSH的环境变量export ZSH=/Users/lorialex/.oh ...

  6. jenkins-node-pipeline

    Jenkins node创建 1.jenkins搭建参考我的另外一篇文章:    http://www.cnblogs.com/cuishuai/p/7544775.html 2.搭建完成后登录,选择 ...

  7. rem手机端适配

    <script> document.documentElement.style.fontSize=document.documentElement.clientWidth*100/750+ ...

  8. 1000. A-B

    1000. A-B -----> http://soj.me/1000 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Descripti ...

  9. elasticsearch启动错误解决

    es启动默认不能使用root用户,所以需要新创建一个用户来启动. 启动时可能出现的问题: [1]: max file descriptors [4096] for elasticsearch proc ...

  10. CUBA 使用 Spring 查询接口

    原文链接:https://www.cuba-platform.com/blog/spring-query-interfaces-in-cuba 翻译:CUBA China CUBA-Platform ...