Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)
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.
Example 1:
Input:
["9001 discuss.leetcode.com"]
Output:
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation:
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
Example 2:
Input:
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output:
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation:
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
Notes:
- The length of
cpdomainswill not exceed100. - The length of each domain name will not exceed
100. - Each address will have either 1 or 2 "." characters.
- The input count in any count-paired domain will not exceed
10000. - The answer output can be returned in any order.
Approach #1: C++.
class Solution {
public:
vector<string> subdomainVisits(vector<string>& cpdomains) {
unordered_map<string, int> mp;
vector<string> ans;
for (int i = 0; i < cpdomains.size(); ++i) {
istringstream iss(cpdomains[i]);
string s;
vector<string> temp;
while (getline(iss, s, ' ')) {
temp.push_back(s.c_str());
}
int num = stoi(temp[0]);
string str = temp[1];
temp.clear();
istringstream jss(str);
// get the subdomains
stack<string> stk;
while (getline(jss, s, '.')) {
stk.push(s.c_str());
}
s = stk.top();
stk.pop();
while (!stk.empty()) {
mp[s] += num;
s = stk.top() + '.' + s;
stk.pop();
}
mp[s] += num;
}
for (auto it = mp.begin(); it != mp.end(); ++it) {
string mario = to_string(it->second) + ' ' + it->first;
ans.push_back(mario);
}
return ans;
}
};
In this question I learned how to split string using istringstream and string splice. then stroe the substring with unordered_map.
Weekly Contest 78-------->811. Subdomain Visit Count (split string with space and hash map)的更多相关文章
- 811. Subdomain Visit Count - LeetCode
Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...
- 【Leetcode_easy】811. Subdomain Visit Count
problem 811. Subdomain Visit Count solution: class Solution { public: vector<string> subdomain ...
- 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 ...
- 811. Subdomain Visit Count
这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...
- [Swift]LeetCode811. 子域名访问计数 | Subdomain Visit Count
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
随机推荐
- jni集成第3方third party动态库libwebrtc_audio_preprocessing.so时android.mk的编写
现在要调用一个第3方库libwebrtc_audio_preprocessing.so 最终布局如下: 即创建2个文件夹,将第3方库放进去. include目录放第3方库的头文件 其中lib目录如下 ...
- ReentrantLock和Synchronized
1 synchronized 1.1 一旦没有获取到就只能一直等待 A和B都获取同一个对象锁,如果A获取了,B没有获取到,那么在A释放该锁之前,B只能无穷等待下去. 1.2 synchronized是 ...
- 今日头条Go建千亿级微服务的实践
今日头条Go建千亿级微服务的实践_36氪 http://36kr.com/p/5073181.html
- java内存泄露具体解释
非常多人有疑问,java有非常好的垃圾回收机制,怎么会有内存泄露?事实上是有的,那么何为内存泄露?在Java中所谓内存泄露就是指在程序执行的过程中产生了一些对象,当不须要这些对象时,他们却没有被垃圾回 ...
- 【C++基础学习】Vector
代码练习: #include <iostream> #include <vector> using namespace std; int main(){ cout <&l ...
- 对于iPhone描述文件的签名认证
1.购买SSL证书验证(跟https认证一样) 2.iphone 签名.mobileconfig文件 company.mobileconfig 未签名的mobileconfig文件 server.cr ...
- HDU2512 一卡通大冒险 —— 第二类斯特林数
题目链接:https://vjudge.net/problem/HDU-2512 一卡通大冒险 Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- CSU - 1530 Gold Rush —— 二进制
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1530 对于一块2^n质量的gold.需要把它分成a质量和b质量(a+b=2^n),且 ...
- Appium——adb 启动问题Invalid argument: cannot open transport registration socketpair could not read ok from ADB Server failed to start daemon * error: cannot connect to daemon
adb启动问题:Invalid argument: cannot open transport registration socketpair could not read ok from ADB S ...
- Dubbo与Zookeeper、SpringMVC整合与使用(干货-理论放一遍。。。还未完结!)
Dubbo跟Zookeeper的简介分享两个不错的链接: Dubbo简介:http://shiyanjun.cn/archives/325.html Zookeeper简介:http://www.op ...