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 exceed 100.
  • 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)的更多相关文章

  1. 811. Subdomain Visit Count - LeetCode

    Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...

  2. 【Leetcode_easy】811. Subdomain Visit Count

    problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...

  3. LeetCode 811 Subdomain Visit Count 解题报告

    题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...

  4. [LeetCode&Python] Problem 811. Subdomain Visit Count

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  5. 【LeetCode】811. Subdomain Visit Count 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...

  6. LeetCode 811. Subdomain Visit Count (子域名访问计数)

    题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...

  7. 811. Subdomain Visit Count (5月23日)

    解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...

  8. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  9. [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

随机推荐

  1. WPF中的数据绑定Data Binding使用小结

    完整的数据绑定的语法说明可以在这里查看: http://www.nbdtech.com/Free/WpfBinding.pdf MSDN资料: Data Binding: Part 1 http:// ...

  2. OpenKM安装(CentOS6)

    OpenKM全称是Open Knowledge Management,是一个DMS(文档管理系统).本文介绍如何在CentOS下安装它.本文的安装程序和资料全部来自OpenKM官网:http://ww ...

  3. Js 模拟鼠标点击事件

    var obj = document.getElementById('go'); if(document.all){ obj.click(); }else{ var e = document.crea ...

  4. kbmmw 5.09 发布

    New stuff        =========        - Added kbmMWSmartBind.pas unit with optional kbmMWSmartBindVCL.pa ...

  5. EasyRTMP实现的一套简单、高效、易用的全平台(Windows/Linux/ARM/Android/iOS)RTMP直播推送库

    本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52938980 EasyRTMP介绍 Easy ...

  6. The Apache Thrift API client/server architecture

    http://thrift.apache.org/ The Apache Thrift software framework, for scalable cross-language services ...

  7. UI标签库专题四:JEECG智能开发平台 Upload(上传标签)

     1. Upload(上传标签) 1.1.  參数 属性名 类型 描写叙述 是否必须 默认值 id string 上传控件唯一标示 是 null name string 控件name 是 null ...

  8. Nodejs通过Thrift操作hbase卡住原因分析及与javascript的垃圾回收机制的关系

    在最近使用Nodejs通过Thrift操作hbase的时候写了个脚本,不断发送http请求,从而取得hbase下所需的数据,但是在run的过程中for循环并没有执行完全,在执行一部分后会卡住,就再也进 ...

  9. ReentrantReadWriteLock读写锁实现分析

    排他锁在同一时刻只允许一个线程进行访问,而读写锁在同一时刻允许多个读线程访问,但是在写线程访问时,所有的读线程和其他的写线程均被阻塞.读写锁内部维护了一对锁,一个读锁和一个写锁,通过分离读锁和写锁,使 ...

  10. docker安装mysql挂载宿主本地目录资源后无法启动的问题

    可能是权限问题,添加--privileged=true参数: docker run -p : --name zsmysql -v $PWD/data:/var/lib/mysql -v $PWD/lo ...