Leetcode __811. 子域名访问计数

问题描述

一个网站域名,如”discuss.leetcode.com”,包含了多个子域名。作为顶级域名,常用的有”com”,下一级则有”leetcode.com”,最低的一级为”discuss.leetcode.com”。当我们访问域名”discuss.leetcode.com”时,也同时访问了其父域名”leetcode.com”以及顶级域名 “com”。

给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:”9001 discuss.leetcode.com”。

接下来会给出一组访问次数和域名组合的列表cpdomains 。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。

示例 1:
输入: 
["9001 discuss.leetcode.com"]
输出: 
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
说明: 
例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com""com"都会被访问,所以它们都被访问了9001次。
示例 2
输入: 
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出: 
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
说明: 
按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。
而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。

注意事项:

  • cpdomains 的长度小于 100。
  • 每个域名的长度小于100。
  • 每个域名地址包含一个或两个”.”符号。
  • 输入中任意一个域名的访问次数都小于10000。

想法

  1. 首先肯定是拆分,把数组拆成一对一对的次数跟url对应的对
  2. 对于每一对而言,把url如何划分出子串,跟数字相互对应,为之后做合并用,为查找快,这里想到的数据结构是Map
  3. 每对url跟数字组成,拆成子串跟数字组合,把相同的子串数目相加,不同的,放入map,最后放入list中返回。

实现

class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        if (cpdomains == null || cpdomains.length == 0) {
            return null;
        }
        int len = cpdomains.length;
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            String[] oneZu = cpdomains[i].split(" ");
            if (oneZu.length == 2) {
                int num = Integer.valueOf(oneZu[0]);
                String oneLine = oneZu[1];
                if(map.keySet().contains(oneLine)){
                    map.put(oneLine, map.get(oneLine) + num);
                }else {
                    map.put(oneLine,num);
                }
                for (int j = 0; j < oneLine.length(); j++) {
                    if (oneLine.indexOf(".") == j) {
                        oneLine = oneLine.substring(j + 1, oneLine.length());
                        if (map.keySet().contains(oneLine)) {
                            map.put(oneLine, map.get(oneLine) + num);
                            j=0;
                        }else {
                            map.put(oneLine, num);
                            j=0;
                        }

                    }
                }
            }
        }
        List<String> list = new ArrayList<>();
        for (String str : map.keySet()) {
            list.add(map.get(str) + " " + str);
        }
        return list;

    }
}

时间复杂度:O(n2)

遇到的问题

1、如何快速的拆分子串?
一个巧妙的递归遍历,出现“.”就截取,再在新串中遍历,下一个“.”,把原串拆分成多个子串

    for (int j = 0; j < oneLine.length(); j++) {
                    if (oneLine.indexOf(".") == j) {
                        oneLine = oneLine.substring(j + 1, oneLine.length());
                        if (map.keySet().contains(oneLine)) {
                            map.put(oneLine, map.get(oneLine) + num);
                            j=0;
                        }else {
                            map.put(oneLine, num);
                            j=0;
                        }

                    }
                }

2、拆分是子串,但本身也是原串的子串,不需要拆分,在第一步原串的基础上就判断map是否有该原串,没有添加,有的话叠加次数。
3、每次拆分完一个子串,j要归零,从新开始,在新的子串上,开始遍历。

优化

参考运行时间较短的程序,发现在每次判断map中是否有该key,再决定往里放还是叠加数字,可以用map的一个函数搞定map.getOrDefault(s, 0)

default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

这样就可以简化为:(思路是一样的,实现方式上,这种更简介)

class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String, Integer> map = new HashMap();
        for (String cd : cpdomains) {
            int i = cd.indexOf(' ');

            int n = Integer.valueOf(cd.substring(0, i));

            String s = cd.substring(i + 1);

            for (i = 0; i < s.length(); ++i) {
                if (s.charAt(i) == '.') {
                    String d = s.substring(i + 1);
                    map.put(d, map.getOrDefault(d, 0) + n);
                }
            }

            map.put(s, map.getOrDefault(s, 0) + n);
        }

        List<String> res = new ArrayList();
        for (String d : map.keySet()) res.add(map.get(d) + " " + d);
        return res;
    }
}

学习,参考~

猜你喜欢

转载自blog.csdn.net/Growing_way/article/details/82355303
今日推荐