zoukankan      html  css  js  c++  java
  • [LeetCode] 811. Subdomain Visit Count

    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.

    子域名访问计数。题意是给一个string array,里面是一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。

    思路是hashmap记录域名和他们分别出现的次数。细节如下,

    首先因为给的有可能是不止一个域名的信息,所以for loop先分开每一个input,比如这样

    [

    "900 google.mail.com",

    "50 yahoo.com",

    "1 intel.mail.com",

    "5 wiki.org"
    ]

    然后再用string array分开每一个全域名的出现次数全域名;接着再来从右向左遍历全域名,并且更新他们的出现次数,比如"900 google.mail.com",需要查看这三个域名。

    com

    mail.com

    google.mail.com

    注意一开始拿到com之后,下一个遍历到的域名是mail,需要跟com做一下string concatenation才能计数。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public List<String> subdomainVisits(String[] cpdomains) {
     3         Map<String, Integer> map = new HashMap<>();
     4         // 遍历每一个全域名信息
     5         for (String cpdomain : cpdomains) {
     6             // 使用空格分隔该信息
     7             String[] arr = cpdomain.split(" ");
     8             // 分割后第一部分为访问次数
     9             int c = Integer.valueOf(arr[0]);
    10             // 第二部分为全域名,将全域名使用.进行分割
    11             String[] domainArr = arr[1].split("\.");
    12             // 子域名
    13             String sub = "";
    14             // 从全域名的最后一段组合子域名
    15             for (int i = domainArr.length - 1; i >= 0; i--) {
    16                 if ("".equals(sub)) {
    17                     sub = domainArr[i];
    18                 } else {
    19                     sub = domainArr[i] + "." + sub;
    20                 }
    21                 // 更新该子域名出现次数
    22                 int count = map.getOrDefault(sub, 0) + c;
    23                 map.put(sub, count);
    24             }
    25         }
    26         // 将map中的数据按照题目要求添加至返回List
    27         List<String> res = new ArrayList<>();
    28         for (String key : map.keySet()) {
    29             res.add(map.get(key) + " " + key);
    30         }
    31         return res;
    32     }
    33 }

    LeetCode 题目总结

  • 相关阅读:
    url参数的获取
    ajax变量作用域的问题
    滚动条自适应宽度的问题
    js发送验证码(倒计时)
    字符串
    值类型和引用类型
    面向对象
    进程
    UI事件
    鼠标跟随
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12847143.html
Copyright © 2011-2022 走看看