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.

  1. Example 1:
  2. Input:
  3. ["9001 discuss.leetcode.com"]
  4. Output:
  5. ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
  6. Explanation:
  7. 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.
  1. Example 2:
  2. Input:
  3. ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
  4. Output:
  5. ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
  6. Explanation:
  7. 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++.

  1. class Solution {
  2. public:
  3. vector<string> subdomainVisits(vector<string>& cpdomains) {
  4. unordered_map<string, int> mp;
  5. vector<string> ans;
  6. for (int i = 0; i < cpdomains.size(); ++i) {
  7. istringstream iss(cpdomains[i]);
  8. string s;
  9. vector<string> temp;
  10. while (getline(iss, s, ' ')) {
  11. temp.push_back(s.c_str());
  12. }
  13. int num = stoi(temp[0]);
  14. string str = temp[1];
  15. temp.clear();
  16. istringstream jss(str);
  17.  
  18. // get the subdomains
  19. stack<string> stk;
  20. while (getline(jss, s, '.')) {
  21. stk.push(s.c_str());
  22. }
  23. s = stk.top();
  24. stk.pop();
  25. while (!stk.empty()) {
  26. mp[s] += num;
  27. s = stk.top() + '.' + s;
  28. stk.pop();
  29. }
  30. mp[s] += num;
  31. }
  32. for (auto it = mp.begin(); it != mp.end(); ++it) {
  33. string mario = to_string(it->second) + ' ' + it->first;
  34. ans.push_back(mario);
  35. }
  36. return ans;
  37. }
  38. };

  

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. JS/PHP字符串截取

    <script> var str="首都医科大学附属北京同仁医院-156"; var index = str.indexOf('-');//获取-的索引值,从0开始算, ...

  2. Activity和ListActivity的区别

    http://book.51cto.com/art/201007/212051.htm

  3. Mongoose学习(3)--设置环境变量

    比如我一套代码数据库代码分为中文站和英文站,每个表中我都有一个site_code字段来区分, 两个站点部署在不同的人服务器,这个时候我们就用系统环境变量来区分, 下面直接在mac下设置环境变量 vim ...

  4. Linux升级安装GCC G++ 6.2

    使用yum安装是不可能了,各大仓库也没有,只能自己编译安装了. 系统为CentOS 6.5,gcc为4.4.7 1 下载源代码包 当前最新版为6.2: wget http://ftp.gnu.org/ ...

  5. zabbix 用户自定义监控参数添加

    1. item  key的添加 key可以带参数,该参数为一个数组列表,可以同时传递多个参数,key的格式如下 key -- [ parameters] -- 例如: vfs.fs.size[/] v ...

  6. ffmpeg视频格式转换中关键帧的设置

    在用ffmpeg转换视频到flv过程中,需要设置关键帧的间隔,以便在播放过程中实现精确定位.在网上查找了不少,最后发现这个指令有效: -g 1 -keyint_min 2 . http://blog. ...

  7. uva 10881 Piotr's Ants 解题报告

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pa ...

  8. Android Studio 使用Gradle多渠道打包

    第一步:配置AndroidManifest.xml 以友盟渠道为例,渠道信息一般都是写在 AndroidManifest.xml文件中,大约如下: <meta-data android:name ...

  9. DGA域名——可以每天只生成一个域名,因此最多存在365个 DGA域名;

    Mirai变种中的DGA 分享到: 发布时间:2016-12-12 16:02:57 作者:360网络安全研究院 投稿方式:发送邮件至linwei#360.cn,或登陆网页版在线投稿 那个导致美国断网 ...

  10. hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)

    题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: ...