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,不过这样看着更清爽。

  • 相关阅读:
    2019-01-28 dubbo安装并启动测试
    JAVAWEB项目获取服务器、项目相关路径方法
    Hibernate中的查询方式
    Hibernate在eclipse中的简单运用
    JQuery Tips(2)----关于$()包装集你不知道的
    JQuery Tips(3)----关于$()包装集内元素的改变
    JQuery Tips(4)----一些关于提高JQuery性能的Tips
    利用FireBug使JQuery的学习更加轻松愉快
    asp.net core2 mvc 基础教程--中间件
    asp.net core2 mvc 基础教程--服务注册和管道
  • 原文地址:https://www.cnblogs.com/boris1221/p/9289980.html
Copyright © 2011-2022 走看看