国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] 811. Subdomain Visit Count

jzman / 2887人閱讀

Problem

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.

Notes:

The length of cpdomains will not exceed 100.
The length of each domain name will not exceed 100.
Each address will have either 1 or 2 "." characters.
The input count in any count-paired domain will not exceed 10000.
The answer output can be returned in any order.

Solution
class Solution {
    public List subdomainVisits(String[] cpdomains) {
        Map map = new HashMap<>();
        for (String domain: cpdomains) {
            String[] cp = domain.split(" ");
            Integer value = Integer.valueOf(cp[0]);
            String key = cp[1];
            while (key.length() > 0) {
                if (!map.containsKey(key)) map.put(key, value);
                else map.put(key, map.get(key)+value);
                
                //O(m*n) = O(key.length*1)
                Integer i = key.indexOf(".");
                if (i > 0) key = key.substring(i+1);
                else break;
            }
        }
        List res = new ArrayList<>();
        for (Map.Entry entry: map.entrySet()) {
            String row = Integer.toString(entry.getValue()) + " " + entry.getKey();
            res.add(row);
        }
        return res;
    }
}
Update with String.indexOf(), people saying it"s faster than String.split()
class Solution {
    public List subdomainVisits(String[] cpdomains) {
        Map map = new HashMap<>();
        for (String domain: cpdomains) {
            int i = domain.indexOf(" ");
            Integer value = Integer.valueOf(domain.substring(0, i));
            String key = domain.substring(i+1);
            while (key.length() > 0) {
                map.put(key, map.getOrDefault(key, 0)+value);
                i = key.indexOf(".");   // if "." not exists, i = -1
                if (i > 0) key = key.substring(i+1);
                else break;
            }
        }
        List res = new ArrayList<>();
        for (Map.Entry entry: map.entrySet()) {
            String row = entry.getValue() + " " + entry.getKey();
            res.add(row);
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/77173.html

相關(guān)文章

  • Leetcode PHP題解--D36 811. Subdomain Visit Count

    摘要:題目鏈接題目分析題目給定一個(gè)字符串?dāng)?shù)組,每個(gè)字符串分兩部分,以空格分割。第一部分為訪問(wèn)次數(shù),第二部分為域名。要求按同樣的格式,分別返回頂級(jí)域名二級(jí)域名三級(jí)域名的訪問(wèn)次數(shù)。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 811. Subdomain Visit Count 題目鏈接 811. Subdomain Visit Count 題目分析 題目給定一個(gè)字符串?dāng)?shù)組,每個(gè)字符串分兩部...

    inapt 評(píng)論0 收藏0
  • 811-子域名訪問(wèn)計(jì)數(shù)

    摘要:作為頂級(jí)域名,常用的有,下一級(jí)則有,最低的一級(jí)為。當(dāng)我們?cè)L問(wèn)域名時(shí),也同時(shí)訪問(wèn)了其父域名以及頂級(jí)域名。輸入中任意一個(gè)域名的訪問(wèn)次數(shù)都小于。 前言 LeetCode上一道不算難的題目,但是一開(kāi)始做的時(shí)候,執(zhí)行時(shí)間很不理想,通過(guò)多次修改代碼,總算是改到比較滿意的地步。原題目如下: 一個(gè)網(wǎng)站域名,如discuss.leetcode.com,包含了多個(gè)子域名。作為頂級(jí)域名,常用的有com,下...

    史占廣 評(píng)論0 收藏0
  • leetcode100 Same Tree 引發(fā)的對(duì)遍歷?算法的思考

    摘要:判斷兩棵樹(shù)是否是相同題目要求傳入兩棵樹(shù)的根節(jié)點(diǎn),判斷這兩棵樹(shù)是否相同此題的核心就在于如何遍歷樹(shù)。定義樹(shù)的一種自然方式是遞歸的方式。一棵樹(shù)是一些節(jié)點(diǎn)的集合,這個(gè)集合可以是空集。這個(gè)遍歷算法可以用于場(chǎng)景如,計(jì)算一個(gè)節(jié)點(diǎn)的高度。 判斷兩棵樹(shù)是否是相同 題目要求:傳入兩棵樹(shù)的根節(jié)點(diǎn),判斷這兩棵樹(shù)是否相同此題的核心就在于如何遍歷樹(shù)。一旦我們解決了這個(gè)問(wèn)題,題目也就迎刃而解了。下面就來(lái)介紹一下 關(guān)...

    cyrils 評(píng)論0 收藏0
  • sqlalchemy使用count時(shí)遇到的坑

    摘要:在用對(duì)一個(gè)千萬(wàn)級(jí)別表進(jìn)行操作時(shí),出現(xiàn)了耗時(shí)嚴(yán)重內(nèi)存飆升的情況。 在用flask-sqlalchemy對(duì)一個(gè)千萬(wàn)級(jí)別表進(jìn)行count操作時(shí),出現(xiàn)了耗時(shí)嚴(yán)重、內(nèi)存飆升的情況。要統(tǒng)計(jì)出一天內(nèi)車(chē)輛訪問(wèn)次數(shù),原代碼如下: car_visit_counts = CarVisit.query.filter( CarVisit.park == car_visit.park, CarVi...

    馬永翠 評(píng)論0 收藏0
  • http cookie解釋

    摘要:可選項(xiàng)部分只會(huì)在瀏覽器端使用,而不會(huì)被發(fā)送至服務(wù)器端。包括過(guò)期時(shí)間選項(xiàng),過(guò)期時(shí)間選項(xiàng)過(guò)期時(shí)間,指定了何時(shí)不會(huì)再被發(fā)送至服務(wù)器,隨后瀏覽器將刪除該。 瀏覽器和Webserver之間的關(guān)系,被設(shè)計(jì)為無(wú)狀態(tài)的,這是一個(gè)很重要的設(shè)計(jì),可以讓客戶(hù)端無(wú)需和服務(wù)器保持狀態(tài),節(jié)省寶貴的端口資源,從而可以為更多的客戶(hù)鏈接服務(wù)。 但是這樣帶來(lái)了問(wèn)題,簡(jiǎn)言之,服務(wù)器無(wú)法知道兩個(gè)請(qǐng)求是否來(lái)自于同一個(gè)瀏覽器。然...

    livem 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<