zoukankan      html  css  js  c++  java
  • [leetcode] 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.

    分析:题目思想比较简单,就是去累加字符串出现的次数。巴拉巴拉,没什么太有算法思想。代码如下:

     1 class Solution {
     2     public List<String> subdomainVisits(String[] cpdomains) {
     3         List<String> list = new ArrayList<>();
     4         Map<String, Integer> map = new HashMap<>();
     5         for ( String s : cpdomains ){
     6             String[] temp = s.split(" ");
     7             int count = Integer.parseInt(temp[0]);
     8             map.put(temp[1],map.getOrDefault(temp[1],0)+count);
     9             int cur = 0;
    10             while ( cur < temp[1].length() ){
    11                 char c = temp[1].charAt(cur);
    12                 if ( c == '.' ){
    13                     String tt = temp[1].substring(cur+1,temp[1].length());
    14                     map.put(tt,map.getOrDefault(tt,0)+count);
    15                 }
    16                 cur++;
    17             }
    18         }
    19         for ( String key : map.keySet() ){
    20             String one = ""+map.get(key)+" "+key;
    21             list.add(one);
    22         }
    23         return list;
    24     }
    25 }

        运行时间18ms,击败了99.41%。如果把map.put(xxxx)写成比较复杂的if else语句,是17ms,不过这样看着更清爽。

  • 相关阅读:
    引用dll文件要提交解决方案
    设置dateBox初始日期为当前日期
    Java基础之线程并发协作(生产消费者模式)
    Java基础之线程的交互
    Java基础之线程synchronized关键字
    Java基础之线程阻塞
    Java基础之线程的两种实现方式
    Java Eclipse代码注释和快捷键
    Java基础之数组
    列出一些你常见的运行时异常?
  • 原文地址:https://www.cnblogs.com/boris1221/p/9289980.html
Copyright © 2011-2022 走看看