Leetcode811 Subdomain Visit Count

由于不熟悉HashMap,这道Easy的题做出了Midium的感觉/笑哭。

大体思路没什么特别的,外循环对Input里的每一条进行处理,内循环把每个Subdomain挑出来,并计数到HashMap中。

方法1:

class Solution {
    public static List<String> subdomainVisits(String[] cpdomains) {
        HashMap<String,Integer> map = new HashMap();
        for(String cpd:cpdomains) {
            String[] temp = cpd.split(" ");
            int count = Integer.valueOf(temp[0]);
            String[] subdomains = temp[1].split("\\.");
            int len = subdomains.length;
            StringBuilder sb = new StringBuilder();
            sb.append(subdomains[len-1]);
            if(map.containsKey(subdomains[len-1])) {
                int v = map.get(subdomains[len-1]);
                map.replace(subdomains[len-1],v+count);
            }
            else {
                map.put(subdomains[len-1],count);
            }
            for(int i=1;i<len;i++) {
                sb.insert(0,‘.‘);
                sb.insert(0,subdomains[len-1-i]);
                String k = sb.toString();
                if(map.containsKey(k)) map.replace(k, map.get(k)+count);
                else map.put(k, count);
            }
        }
        List<String> result = new ArrayList<String>();
        map.forEach((k,v)->{
            StringBuilder sb = new StringBuilder();
            sb.append(v);sb.append(‘ ‘);sb.append(k);
            result.add(sb.toString());
        });
        return result;
    }
}

垃圾的一批…但是看了看代码不好再优化了呀。绝望的试了试map.entrySet(),(其实用map.keySet()方法再map.getKey()也可以,一样的,得到方法2,只优化了最后map的使用,效果显著:

class Solution {
    public static List<String> subdomainVisits(String[] cpdomains) {
        HashMap<String,Integer> map = new HashMap();
        for(String cpd:cpdomains) {
            String[] temp = cpd.split(" ");
            int count = Integer.valueOf(temp[0]);
            String[] subdomains = temp[1].split("\\.");
            int len = subdomains.length;
            StringBuilder sb = new StringBuilder();
            sb.append(subdomains[len-1]);
            if(map.containsKey(subdomains[len-1])) {
                int v = map.get(subdomains[len-1]);
                map.replace(subdomains[len-1],v+count);
            }
            else {
                map.put(subdomains[len-1],count);
            }
            for(int i=1;i<len;i++) {
                sb.insert(0,‘.‘);
                sb.insert(0,subdomains[len-1-i]);
                String k = sb.toString();
                if(map.containsKey(k)) map.replace(k, map.get(k)+count);
                else map.put(k, count);
            }
        }
        List<String> result = new ArrayList<String>();
//        map.forEach((k,v)->{
//            StringBuilder sb = new StringBuilder();
//            sb.append(v);sb.append(‘ ‘);sb.append(k);
//            result.add(sb.toString());
//        });
        Set<Map.Entry<String,Integer>> set = map.entrySet();
        for(Map.Entry<String, Integer> e:set) {
            StringBuilder sb = new StringBuilder();
            sb.append(e.getValue());sb.append(‘ ‘);sb.append(e.getKey());
            result.add(sb.toString());
        }
        return result;
    }
}

之后还可以改进,在获得subdomain时,可以不用StringBuilder相加获得,可以通过substring方法获得,空间开支会小一些,时间开支也少了,方法3:

原文地址:https://www.cnblogs.com/chason95/p/9290047.html

时间: 2024-11-05 23:32:37

Leetcode811 Subdomain Visit Count的相关文章

811.&#160;Subdomain Visit Count - LeetCode

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.

811. 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

[LeetCode&amp;Python] Problem 811. 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

LeetCode 811 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

811. Subdomain Visit Count (5月23日)

解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains) { vector<string> result; map<string,int> pair; for(string str:cpdomains){ auto space=str.find(' '); int temp=stoi(str.substr(0,space)); str=s

白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊.今日重新开始吧,先来一道简单的题目,如下: 这道题首先想到的还是使用Map,代码如下: /** * @param {string[]} cpdomains * @return {string[]} */ var subdomainVisits = function(cpdomains) { let tmp = new Map(); let res = new Array(); for(let i = 0 ; i < cpdomains

[leetcode-811-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

arts-week2

Algorithm 739. Daily Temperatures - LeetCode 535. Encode and Decode TinyURL - LeetCode 811. Subdomain Visit Count - LeetCode 706. Design HashMap - LeetCode 771. Jewels and Stones - LeetCode 204. Count Primes - LeetCode 445. Add Two Numbers II - LeetC

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl