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. java线程中断[interrupt()函数] (转载)

    一个正常的线程中断: 从运行到真正的结束,应该有三个阶段: 正常运行. 处理结束前的工作,也就是准备结束. 结束退出. Java曾经提供过抢占式限制中断,但问题多多,例如的Thread.stop.另一 ...

  2. 有趣的Ruby-学习笔记3

    Ruby方法 方法名要以小写字母开头.假设用大写字母开头会被作为常量 (这点非常奇怪) 定义一个无參的方法 def method_name expr.. end 定义一个有參的方法 def metho ...

  3. C#用Infragistics 导入导出Excel

    最近项目中有数据的导入导出Excel的需求,这里做简单整理. 公司用的是Infragistics的产品,付费,不需要本地安装Office. 有需要的朋友可以下载 Infragistics.2013.2 ...

  4. 【百度之星初赛A】路径交 LCA+线段树

    [百度之星初赛A]路径交 Problem Description 给定一棵n个点的树,以及m条路径,每次询问第L条到第R条路径的交集部分的长度(如果一条边同时出现在2条路径上,那么它属于路径的交集). ...

  5. Angular中的内置指令和自定义指令

    NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...

  6. 创建私有maven库

    在公司搭建私有maven库,我为了在本机做测试,我选择Sonatype nexus oss,在docker里面安装 1.Mac上安装docker 下载地址:https://github.com/boo ...

  7. linux source命令与sh shell scripts的区别

    source FileName 作用:在当前bash环境下读取并执行FileName中的命令. 注:该命令通常用命令“.”来替代. 如:source .bash_rc 与 . .bash_rc 是等效 ...

  8. Redis学习笔记(1):Redis的说明与安装

    Redis学习笔记(1):Redis说明的安装 说明 什么是Redis REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-va ...

  9. Shell脚本学习指南笔记

    Shell脚本学习指南 作者:Danbo 2015-8-3 脚本编程语言与编译型语言的差异 许多中型.大型的程序都是用编译型语言写的,例如:C.C+.Java等.这类程序只要从源代码(Source C ...

  10. LightOJ1197 Help Hanzo —— 大区间素数筛选

    题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo    PDF (English) Statistics Forum Tim ...