Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)
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 exceed100
. - 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.
Approach #1: C++.
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> mp;
vector<string> ans;
for (int i = 0; i < cpdomains.size(); ++i) {
istringstream iss(cpdomains[i]);
string s;
vector<string> temp;
while (getline(iss, s, ' ')) {
temp.push_back(s.c_str());
}
int num = stoi(temp[0]);
string str = temp[1];
temp.clear();
istringstream jss(str); // get the subdomains
stack<string> stk;
while (getline(jss, s, '.')) {
stk.push(s.c_str());
}
s = stk.top();
stk.pop();
while (!stk.empty()) {
mp[s] += num;
s = stk.top() + '.' + s;
stk.pop();
}
mp[s] += num;
}
for (auto it = mp.begin(); it != mp.end(); ++it) {
string mario = to_string(it->second) + ' ' + it->first;
ans.push_back(mario);
}
return ans;
}
};
In this question I learned how to split string using istringstream and string splice. then stroe the substring with unordered_map.
Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)的更多相关文章
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- [LeetCode&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- 【LeetCode】811. Subdomain Visit Count 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- 811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...
- [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
随机推荐
- javascript 中 "undefined" 与 "is not defined" 分析
var var1; console.log( typeof var0);//print "undefined",主要看下面对var0单独的输出 console.log( typ ...
- 我们将要建立的EasyDarwin开源社区
从12年12月我开始建立EasyDarwin开源项目,已经三年多的时间了,从开始最简单的一个开源流媒体服务器项目,如今已经发展成为目前国内最大的一个流媒体开源社区,截至目前已经有十几个项目在Githu ...
- Redis(二)延迟队列
1.目录 延迟队列 进一步优化 2.延迟队列 package com.redis; import java.lang.reflect.Type; import java.util.Set; impor ...
- ElasticSearch(十五) _search api 分页搜索及deep paging性能问题
1.分页搜索 语法: size,from GET /_search?size=10 GET /_search?size=10&from=0 GET /_search?size=10&f ...
- iOS 判定string是不是中文字符
+(BOOL)IsChinese:(NSString *)str { ; i< [str length];i++) { int a = [str characterAtIndex:i]; if( ...
- Linux集群基础
Linux集群基础 作者:Danbo 时间:2015-7-12 集群概述 什么是集群?集群是一组协同工作的服务器实体.用以提供比单一服务实体更具扩展性和可用性的平台. 集群的分类 1.HPC(High ...
- html5学习笔记(1)-新标签
最近在做的项目中用到了Html5的部分标签,经师父提醒感觉自己用section的次数多的有点过分,今天去找了一篇HTML5新标签的使用方法,特意贴了上来,感谢原作者的分享,方便以后自己使用~~~ HT ...
- 人生苦短之Python的urllib urllib2 requests
在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库 ...
- ArcGIS发布动态空间,并验证
发布 发布方法见视频. 验证 发布动态空间后,页面底部有 点进去后,使用如下语法验证. {"id": 0,"source": {"type" ...
- numpy.argmax 用在求解混淆矩阵用
numpy.argmax numpy.argmax(a, axis=None, out=None)[source] Returns the indices of the maximum values ...