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、代码

 1 vector<string> subdomainVisits(vector<string>& cpdomains) {
 2         // 思路可以将字符串先分割,然后使用 unorder_set 计数
 3         // 使用一个子函数将每个cpdomains 分割
 4
 5         unordered_map<string,int> m;
 6
 7
 8         for(auto Itr = cpdomains.begin() ; Itr != cpdomains.end(); Itr++ )
 9         {
10             vector<string> Sproc = split_cpdomains(*Itr);
11             for(auto itr = Sproc.begin() + 1; itr != Sproc.end(); itr++ )
12             {
13                 m[*itr] += stoi(Sproc[0]);
14             }
15         }
16
17         vector<string> ans;
18         for(auto it = m.begin() ; it != m.end(); it++ )
19         {
20             ans.push_back( to_string(it->second) +" "+ it->first );
21         }
22
23         return ans;
24
25
26     }
27
28     vector<string> split_cpdomains(string & s)
29     {
30         vector<string> res;
31
32         int i = 0;
33         for(auto itr = s.begin() ; itr != s.end() ; itr++)   // 以下代码可以用 string 的find() 成员函数简化
34         {
35             i++;
36             if( *itr == ‘ ‘)
37             {
38                 res.push_back( s.substr(0,i )); // put number in
39                 break;
40             }
41         }
42
43
44         res.push_back( s.substr( s.find_first_of(‘ ‘) + 1 )) ;
45         res.push_back(  s.substr(s.find_first_of(".") +1 ));
46         if( s.find_first_of(".") != s.find_last_of(".") )
47             res.push_back( s.substr( s.find_last_of(".") + 1) ) ;
48
49
50         return res;
51     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/8710886.html

时间: 2024-11-10 12:45:21

leetcode题解之分解字符串域名的相关文章

Leetcode题解——数据结构之字符串

1. 字符串循环移位包含 2. 字符串循环移位 3. 字符串中单词的翻转 4. 两个字符串包含的字符是否完全相同 5. 计算一组字符集合可以组成的回文字符串的最大长度 6. 字符串同构 7. 回文子字符串个数 8. 判断一个整数是否是回文数 9. 统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数 1. 字符串循环移位包含 编程之美 3.1 s1 = AABCD, s2 = CDAA Return : true 给定两个字符串 s1 和 s2,要求判定 s2 是否能够被 s1 做循环

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode 题解: Length of Last Word

leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non

[LeetCode 题解]:Palindrome Number

前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of c

LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)

LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 简单 二叉树 序号 题目 难度 07 重建二叉树 中等 栈和队列 序号 题目 难度 09 用两个栈实现队列 简单 图 序号 题目 难度 12 矩阵中的路径 中等 13 机器人的运动范围 中等 算法 动态规划 序号 题目 难度 10- I 斐波那契数列 简单 10- II 青蛙跳台阶问题 简单 查找

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现: