811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域。
方法一,查找标志字符,用标志字符把字符串分成几段
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的更多相关文章
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- LeetCode 811 Subdomain Visit Count 解题报告
题目要求 A website domain like "discuss.leetcode.com" consists of various subdomains. At the t ...
- [LeetCode&Python] Problem 811. Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- 【LeetCode】811. Subdomain Visit Count 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计次数 日期 题目地址:https://lee ...
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- [LeetCode] Subdomain Visit Count 子域名访问量统计
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
随机推荐
- CentOS Netstat命令
语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址: -c或--conti ...
- Watcher、ZK状态、事件类型 ,权限
zookeeper有watch事件,是一次性触发的,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher. 同样,其watcher是监听数据发送了某些变化,那就一定 ...
- selenium模拟事件处理
执行原理: 调用ActionChains的方法时不会立即执行,会将所有的操作按顺序存放在一个队列里,当调用perform()方法时,从队列中的事件会依次执行. 支持链式写法或者是分布写法. 鼠标键盘方 ...
- Debug和汇编编译器masm对指令的不同处理
我们在Debug和源程序中写入同样形式的指令 : "mov al,[0]","mov bl,[1]","mov cl,[2]"," ...
- Django中使用django_debug_toolbar
一 概述 django_debug_toolbar 是django的第三方工具包,给django扩展了调试功能. 包括查看执行的sql语句,db查询次数,request,headers,调试概览等. ...
- 包含了重复的“Content”项。.NET SDK 默认包含你项目目录中的“Content”项。可从项目文件中删除这些项;如果希望将其显式包含在项目文件中,可将“EnableDefaultContentItems”属性设置为“false”
从.netcore 1.1 升级到2.0时遇到该问题. 参考http://www.cnblogs.com/xishuai/p/visual-studio-for-mac.html 根据提示可知(我是看 ...
- Vue 1.0 和 2.0 执行顺序
// Vue 生命周期 // Vue 1.0 // 执行步骤:选项/生命周期钩子 // 1 init // 2 created // 3 beforeCompile // 4 compiled // ...
- 基于Confluent.Kafka实现的KafkaConsumer消费者类和KafkaProducer消息生产者类型
一.引言 研究Kafka有一段时间了,略有心得,基于此自己就写了一个Kafka的消费者的类和Kafka消息生产者的类,进行了单元测试和生产环境的测试,还是挺可靠的. 二.源码 话不多说,直接上代码,代 ...
- C# 解决SharpSvn启动窗口报错 Unable to connect to a repository at URL 'svn://....'
在远程机打开sharpsvn客户端测试,结果报错 Svn启动窗口报错 Unable to connect to a repository at URL 'svn://...' 咋整,我在win10我的 ...
- Python while for if....else
1函数input()的工作原理: 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量(即要向用户显示的提示或说明) raw_input 是Py ...