解答

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> result;
        map<string,int> pair;
        for(string str:cpdomains){
            auto space=str.find(' ');
            int temp=stoi(str.substr(0,space));
            str=str.substr(space+1);
            while(!str.empty()){
                 pair[str] += temp;
                 if(str.find('.')!=string::npos){
                     str=str.substr(str.find('.')+1);
                 }
                 else{
                     str.clear();
                 }
            }
        }
        for(auto temp:pair){
             result.push_back(to_string(temp.second)+' '+temp.first);
        }
        return result;
    }
};

此题个人有思路,但无从下手,是参考他人解法后从而解出来的。

参考链接:https://leetcode.com/problems/subdomain-visit-count/discuss/129879/C++-O(sum-of-lengths-of-strings)-simple-Solution

笔记

  1. string类型中的find,substr成员函数的使用
  2. 字符串转整数函数stoi,整数转字符串函数to_string

函数学习

1. string::find 查找

参考C++ Primer 5th 9.5.3节

1. 函数原型,有四个重载版本
// 1
size_type find( CharT ch, size_type pos = 0 ) const;
// 2
size_type find( const basic_string& str, size_type pos = 0 ) const
// 3
size_type find( const CharT* s, size_type pos = 0 ) const;
// 4
size_type find( const CharT* s, size_type pos, size_type count ) const;
2. 解释

返回类型:是一个size_type类型,是一个整数,表示匹配位置的下标;如果没有找到,则会返回string::npos,这是一个确定值,类型是整数,初始值为-1
查找开始位置:即pos,在1,2,3中默认从开始查找
前三个版本解释:1。查找字符;2. 查找string类型字符串;3.查找C风格字符串
第四个版本解释:查找一个C风格字符串的前n个字符字符,同时开始查找位置pos需指定

3. 示例程序
/********************************************
*
* 程序作用:测试string类型中的find成员函数
*
* a11测试第一个重载版本,能够找到;
* a12测试第一个重载版本,但不能找到
* a21测试第二个重载版本,能够找到;
* a22测试第二个重载版本,但不能找到
* a3 测试第三个重载版本,但不能找到
* a4 测试第四个重载版本,能够找到
*
* 编 制 人:niaocaics
* 编制时间: 2018.5.23
* 联系邮箱:niaocaics@163.com
*
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    string s{ "Hello World" };
    size_t a11 = s.find(' '),a12=s.find(' ',7);
    size_t a21 = s.find("lo"),a22=s.find("lol");
    const char * str = "lol";
    size_t a3 = s.find(str);
    size_t a4 = s.find(str,1,2);
    cout << a11 << " " << a12 << endl << a21 << " " << a22
        << endl << a3 << endl << a4 << endl;
    system("pause");
    return 0;
}

输出结果
环境:Visual Studio 2017
输出:

5 4294967295
3 4294967295
4294967295
3

由此可见,npos会返回-1,因为size_t是一个无符号整数,所以整数-1会被解析成最大数

2. string::substr 求子串

参考C++ Primer 5th 9.5.1节

1. 函数原型:
basic_string substr( size_type pos = 0,
                size_type count = npos ) const;
2. 解释

pos代表开始位置,默认会从字符串开头开始;
count代表复制的字符数,默认为npos,但由于size_type是无符号整数,因此有符号的-1会被解析成最大数,即count=最大数即到字符串结尾

3. 示例程序
/****************************************************************
* 程序作用:测试string类型中的substr成员函数
*
* s1使用默认参数
* s2只对开始位置进行处理
* s3指定开始字符,和字符长度(在字符串范围内)
* s3指定开始字符,和字符长度(但大于在字符串原本长度)
*
* 编 制 人:niaocaics
* 编制时间: 2018.5.24
* 联系邮箱:niaocaics@163.com
*
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    string s = { "Hello World!" };
    string s1 = s.substr();
    string s2 = s.substr(6);
    string s3 = s.substr(6, 3);
    string s4 = s.substr(6, 100);
    cout << s1 << endl << s2 << endl <<
            s3 << endl << s4 << endl;
    system("pause");
    return 0;
}

输出结果
环境:Visual Studio 2017
输出:

Hello World!
World!
Wor
World!

3 stoi 将字符串转化为整数

1 函数原型
int stoi( const std::string& str,
        std::size_t* pos = 0, int base = 10 );

字符串有俩种选择,宽字符串wstring和一般string,
返回类型有三种,int;long;long long

2 解释

str是要转换的字符串,pos代表开始位置默认从字符串,base代表进制,默认十进制

3 示例程序
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    string s = { "1234567" };
    int a = stoi(s);
    cout << a << endl;
    system("pause");
    return 0;
}

输出:1234567

4. to_string 把数转换成字符串(C++11)

std::string to_string( int value );

value可以是int,long,long long,unsigned int,unsigned long,unsigned long long,float,double,long double类型

2 解释

value是数字,返回一个字符串

3 示例程序
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    int a1 = 23456;
    float a2 = 3.1415;
    string s1 = to_string(a1);
    string s2 = to_string(a2);
    cout << s1 << endl << s2 << endl;
    system("pause");
    return 0;
}

输出:
23456
3.141500

总结

此题暴露了我对库函数的不熟悉,以后要多了解库函数

811. Subdomain Visit Count (5月23日)的更多相关文章

  1. 【Leetcode_easy】811. Subdomain Visit Count

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

  2. 811. Subdomain Visit Count - LeetCode

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

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

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

  4. LeetCode 811 Subdomain Visit Count 解题报告

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

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

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

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

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

  7. 811. Subdomain Visit Count

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

  8. 2016年12月23日 星期五 --出埃及记 Exodus 21:18

    2016年12月23日 星期五 --出埃及记 Exodus 21:18 "If men quarrel and one hits the other with a stone or with ...

  9. [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 )

    [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 ) billcheung 发表于 2011-10-23 00:07:49 https://www.itsk.com ...

随机推荐

  1. 001服务注册与发现Eureka

    1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka Server依赖和Spring Cloud依赖管理 <dependencies> <!--添加Eurek ...

  2. 如何优雅地使用Markdown (Sublime 3 + MarkdownEditing+OmniMarkupPreviewer)

    最近开始上手Sublime 3 作为Markdown 的重度使用者自然关于Markdown的插件是必不可少的 . 在这里记录分享一下我常用的两款Markdown插件. MarkdownEditing ...

  3. 【转】Web服务器之Nginx详解(理论部分)

    大纲 一.前言 二.Web服务器提供服务的方式 三.多进程.多线程.异步模式的对比 四.Web 服务请求过程 五.Linux I/O 模型 六.Linux I/O 模型具体说明 七.Linux I/O ...

  4. Linux Firefox Adobe Flash Player 安装和更新

    1.下载 Firefox Adobe Flash Player 使用Linux上的火狐浏览器访问如下的下载网址: https://get.adobe.com/flashplayer/ 选择下载 &qu ...

  5. android测试Code

    <!--android:layout_alignParentTop="true"--><com.koooke.platform.View.CenterImage ...

  6. QT的hint的toolTip的使用

    QString value = ''1213213231"; this->setToolTip(value);//QT自带的接口 value就是自己想要塞进的数据. 如果字符多的话 怎 ...

  7. 沉淀,再出发:Git的再次思考

    沉淀,再出发:Git的再次思考 一.前言 使用git也有很久了,后来有一段时间一直没有机会去使用,现在想来总结一下自己学习了这么长时间的一些心得感悟,我写的博客一般都是开了一个轮廓和框架,等到以后有所 ...

  8. 设计模式:备忘录(Memento)模式

    设计模式:备忘录(Memento)模式 一.前言   备忘录模式用于保存和恢复对象的状态,相信大家看过我前面的拙作就会想到原型模式也能保存一个对象在某一个时刻的状态,那么两者有何不同的呢?原型模式保存 ...

  9. bep-10翻译

    dht协议的目的是解放tracter服务器,将tracter的任务分布式存到各个客户端上(即维护资源文件的下载列表,从哪能下载到请求的文件): dht协议在get_peer请求获得peer信息后,就会 ...

  10. SAP HANA Delivery Unit概念简述

    介绍 在SAP HANA应用开发领域里,我们通常用package来存储modeler views和XS工程等模型.这些包应该被部署到最终的生产服务器上. Delivery Unit是SAP HANA原 ...