zoukankan      html  css  js  c++  java
  • 811. Subdomain Visit Count

    Question

    811. Subdomain Visit Count

    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.
    

    Solution

    题目大意:统计访问子域名的次数

    思路:遍历每个域名及其子域名,将其访问次数保存到map里

    Java实现:

    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String, Integer> map = new HashMap<>();
        for (String str : cpdomains) {
            String[] arr = str.split(" ");
            int baseCount = Integer.parseInt(arr[0]);
            String tmpSub = arr[1];
            int fromIndex = 0;
            while (fromIndex != -1) {
                // System.out.println(fromIndex);
                String subDomain = tmpSub.substring(fromIndex + (fromIndex != 0 ? 1 : 0));
                Integer oldCount = map.get(subDomain) == null ? 0 : map.get(subDomain);
                map.put(subDomain, oldCount + baseCount);
                fromIndex = tmpSub.indexOf(".", fromIndex + 1);
            }
        }
    
        List<String> retList = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            retList.add(entry.getValue() + " " + entry.getKey());
        }
        return retList;
    }
    
  • 相关阅读:
    两台Mysql数据库数据同步实现
    利用Python读取外部数据文件
    在Python应用中使用MongoDB
    使用python语言操作MongoDB
    windows下Graphviz安装及入门教程
    【Machine Learning】决策树案例:基于python的商品购买能力预测系统
    Python数据可视化-seaborn
    np.tile 函数使用
    Python机器学习库scikit-learn实践
    基于C#net4.5websocket客户端与服务端
  • 原文地址:https://www.cnblogs.com/okokabcd/p/9379940.html
Copyright © 2011-2022 走看看