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

  • 相关阅读:
    Dart中的类型转换总结:
    【Dart学习】--Dart之数组(List)的相关方法总结
    Navigator的使用:
    001——Angular环境搭建、运行项目、搭建项目
    Dart中的数据类型转换:
    Flutter中的Stack、Align、Positioned的使用
    Flutter设置图片为正方形
    顶部导航TabBar、TabBarView、DefaultTabController
    《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <二> NSMutableString
    Swift随记
  • 原文地址:https://www.cnblogs.com/boris1221/p/9289980.html
Copyright © 2011-2022 走看看