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

  • 相关阅读:
    ReactiveCocoa入门教程——第一部分【转载】
    浅谈iOS中MVVM的架构设计与团队协作【转载】
    如何使用CocoaPods安装使用及配置私有库以及管理依赖库 【原创】
    工作经验【原创】
    xcode常见报错调试【原创】
    彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题(转)
    Android遇到的那些坑
    Android开发和Android Studio使用教程
    CGI是什么 搜索了这么多,大致看明白了保留下来。
    不同手机根据坐标计算控件、图片的像素,px 与 dp, sp换算公式?
  • 原文地址:https://www.cnblogs.com/boris1221/p/9289980.html
Copyright © 2011-2022 走看看