leetcode题解之分解字符串域名
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题解之分解字符串域名的更多相关文章
- leetCode题解之反转字符串中的元音字母
1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- leetcode题解(持续更新)
leetcode题解 1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode题解汇总(包括剑指Offer和程序员面试金典,持续更新)
LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...
- LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)
LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...
随机推荐
- Android 开发工具类 20_DOMPersonService
xml 文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <person i ...
- Mac OS配置Android SDK环境变量(当不存在 .bash_profile 文件的时候)
苹果系统已经包含完整的J2SE,其中就有JDK和JVM(苹果叫VM). 如果要在MAC系统下开发CODE.可以先装个IDE(NETBEANS/Eclipse等),而后不需要装JDK和JVM了,MAC下 ...
- LetExpr表达式解读
Integer a = 0; a++; 举个例子,如下: package com.test19; public class BugReport<T> { private T n; publ ...
- android studio生成aar包并在其他工程引用aar包
1.aar包是android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android studio 工程后,其他工程可以方便引用源码和资源文件 ...
- linux安装tmux
由于tmux依赖于libevent和ncurses-devel,所以应首先有这两个库,和相关的头文件. 1.对于ncurses-devel,可用yum安装. yum install ncurses-d ...
- LDAP落地实战(一):OpenLDAP部署及管理维护
公司内部会有许多第三方系统或服务,例如Svn,Git,VPN,Jira,Jenkins等等,每个系统都需要维护一份账号密码以支持用户认证,当然公司也会有许多的主机或服务器,需要开放登录权限给用户登录使 ...
- Behave 基础
在你使用behave或其他BDD框架之前, 你应该选择一个断言库. python有很多这方面的第三方库.例如: hamcrest,nose.tools, should-dsl, sure, comp ...
- Resources in Visual Tracking
这个应该是目前最全的Tracking相关的文章了 一.Surveyand benchmark: 1. PAMI2014:VisualTracking_ An Experimental Sur ...
- Firebird 获取用户表及字段
select rdb$relation_fields.rdb$relation_name table_name, rdb$relations.rdb$description table_des, rd ...
- CentOS如何挂载U盘(待更新)
使用Linux系统时,经常需要用到U盘,下面介绍以下如何再CentOS上挂载U盘. 首先,切换到root用户. 首先,切换到root用户. 首先,切换到root用户. 重要的事情说三遍,很多同学都说, ...