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

方法一,查找标志字符,用标志字符把字符串分成几段

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
vector<string> subdomainVisits(vector<string>& cpdomains)
{
unordered_map<string,int> simap;
for(string &s:cpdomains)
{
int i=s.find(" ");
int times=stoi(s.substr(,i));
string ss=s.substr(i+,s.size()-i-);
simap[ss]+=times;
int sssz=ss.size();
for(int i=;i<sssz;i++)
{
if(ss[i]=='.')
simap[ss.substr(i+,sssz-i-)]+=times;
}
}
vector<string>res;
for(auto &p:simap)
res.push_back(to_string(p.second)+" "+p.first);
return res;
}
};

方法二,用stringstream

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
vector<string> subdomainVisits(vector<string>& cpdomains)
{
unordered_map<string,int> simap;
for(string &s:cpdomains)
{
stringstream ss;
int cnt=;
string dom;
ss<<s;
ss>>cnt>>dom;
simap[dom]+=cnt;
int sz=dom.size();
for(int i=;i<sz;i++)
{
if(dom[i]=='.')
simap[dom.substr(i+,sz-i-)]+=cnt;
}
}
vector<string> res;
for(auto &p:simap)
res.push_back(to_string(p.second)+" "+p.first);
return res;
}
};

用stringstream的方法速度要慢一些

811. Subdomain Visit Count的更多相关文章

  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 解题报告

    题目要求 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. [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count

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

  9. [LeetCode] Subdomain Visit Count 子域名访问量统计

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

随机推荐

  1. LibreOJ 6277. 数列分块入门 2

    题目链接:https://loj.ac/problem/6278 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 这题我 ...

  2. vue项目引入FastClick组件解决IOS系统下h5页面中的按钮点击延迟,连续点击无反应的问题

    异常描述: ios系统手机中访问h5页面,按钮点击有延迟,连续点击卡顿.无反应. 异常原因: 这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题:当时的网站都是为大屏幕设 ...

  3. winbox添加dhcp和nat

    例子如上,wan连接外网,lan作为内网. 设置网卡ip 保证mac地址正确! 去到ip address里,设置两网卡的ip! 保证能ping通外网! 设置dhcp ip pool添加池,192.16 ...

  4. 项目总结01:JSP mysql SpringMvc下中国省市县三级联动下拉框

    JSP mysql SpringMvc下中国省市县三级联动下拉框 关键词 JSP  mysql数据库  SpringMvc  ajax   Controller层  Service层  中国地区  省 ...

  5. 为什么使用Reazor

    原因:类似于前边写的模板页,自己写了.还需要用replace来替换成自己想要的变量.. 常见的模板引擎:Razor.Nvelocity.Vtemplate. Razor有VS自动提示,而且有助于学习a ...

  6. 以太坊测试网络搭建以及RPC服务开启-配置注意事项

    .Eth QA Test环境: 数据文件路径: C:\Users\Administrator\AppData\Roaming\Ethereum console启动参数: --rpc --testnet ...

  7. IIS 7.5 上传文件大小限制

    上传插件:uploadify IIS版本:7.5 描述: 从IIS6升级到IIS7.5以后,网站上传文件大小被限制了,在Chrome下提示:ERR_CONNECTION_RESET,网上的各种方法都试 ...

  8. vue-awesome-swiper轮播的使用

    一.安装vue-awesome-swiper npm install vue-awesome-swiper --save 二.引入插件 main.js里面分别引入(记得有些电脑要引入样式) impor ...

  9. jquery插件之选项卡

    jQuery插件编写 首先来一个简拓展jQuery对象的方法 <body > <p>23</p> <script src="js/jquery-1. ...

  10. OCSP

    一.简介   二.协议   三.其他 1)OCSP装订 https://zh.wikipedia.org/wiki/OCSP%E8%A3%85%E8%AE%A2