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个节点 ... 
随机推荐
- webpack处理url资源的配置
			webpack处理url资源的配置 1.安装 npm i url-loader -D 2.修改webpack.config.js const path = require('path'); // 启用 ... 
- Spring Boot 日志配置
			Spring Boot 日志配置 默认日志 Logback: 默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台.在运行应用程序和其他例子时,你应该已经看到很 ... 
- CentOS 配置使用 EPEL YUM 源
			EPEL(Extra Packages for Enterprise Linux)是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS. ... 
- 【树】Binary Tree Right Side View
			题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ... 
- Java性能调优:利用JMC进行性能分析
			JMC, 即Java任务控制(Java Mission Control)是从Java7(7u40)和 Java8 的商业版本包括一项新的监控和控制特性. JMC 程序 (JDK_HOME\bin目录下 ... 
- 14-hadoop-运行的2种方式
			hadoop的程序运行有, 具体有3种, 除了上一篇(http://www.cnblogs.com/wenbronk/p/6662119.html)提过的直接发布jar上传到hadoop以外, 还有本 ... 
- Linux-(diff)
			diff 命令 1.命令格式: diff [参数] [文件1或目录1] [文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时 ... 
- JS的可枚举性
			在学习ES6的过程中,涉及到遍历方法时,提到过可枚举性,且多种遍历方法都与可枚举性相关.本章节,将总结这些遍历方法的可枚举性,并在必要的部分,给出对比实例. 一.设置属性的可枚举性 在上一文章 ... 
- 不会几个框架,都不好意思说搞过前端: Node.js & angular.js
			Node.js 菜鸟教程 :http://www.runoob.com/nodejs/nodejs-install-setup.html angular.js 菜鸟教程 :http://www.r ... 
- elasticsearch java使用
			本文将介绍如何使用java调用elasticsearch的api 1.获取client连接 package com.test.elasticsearch; import java.net.InetAd ... 
