17,226
社区成员




网站域名 "discuss.leetcode.com" 由多个子域名组成。顶级域名为 "com" ,二级域名为 "leetcode.com" ,最低一级为 "discuss.leetcode.com" 。当访问域名 "discuss.leetcode.com" 时,同时也会隐式访问其父域名 "leetcode.com" 以及 "com" 。
计数配对域名 是遵循 "rep d1.d2.d3" 或 "rep d1.d2" 格式的一个域名表示,其中 rep 表示访问域名的次数,d1.d2.d3 为域名本身。
例如,"9001 discuss.leetcode.com" 就是一个 计数配对域名 ,表示 discuss.leetcode.com 被访问了 9001 次。
给你一个 计数配对域名 组成的数组 cpdomains ,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。
示例:
输入:cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出:["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
解释:按照前文描述,会访问 "google.mail.com" 900 次,"yahoo.com" 50 次,"intel.mail.com" 1 次,"wiki.org" 5 次。
而对于父域名,会访问 "mail.com" 900 + 1 = 901 次,"com" 900 + 50 + 1 = 951 次,和 "org" 5 次。
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
Map<String,Integer> map = new HashMap<String,Integer>();
List<String> list = new ArrayList<String>();
String[] chs;
String[] chs1;
int t=0;
for(String s:cpdomains){
chs = s.split("\s+");
t = Integer.parseInt(chs[0]);
chs1 = chs[1].split("\\.");
if(chs1.length==2){
map.put(chs1[1],map.getOrDefault(chs1[1],0)+t);
} else if(chs1.length==3) {
map.put(chs1[2],map.getOrDefault(chs1[2],0)+t);
map.put(chs1[1]+"."+chs1[2],map.getOrDefault(chs1[1]+"."+chs1[2],0)+t);
}
map.put(chs[1],map.getOrDefault(chs[1],0)+t);
}
for(String key:map.keySet()){
list.add(map.get(key)+" "+key);
}
return list;
}
}