Leetcode 811. Subdomain Visit Count

class Solution(object):
    def subdomainVisits(self, cpdomains):
        """
        :type cpdomains: List[str]
        :rtype: List[str]
        """
        count = {}
        for cpdom in cpdomains:
            num, dom = cpdom.split()
            num = int(num)
            i = dom.rfind('.')
            while i != -1:
                count[dom[i + 1:]] = count.get(dom[i + 1:], 0) + num
                i=dom[0:i].rfind('.')
            count[dom] = count.get(dom, 0) + num
        ans = []
        for k, v in count.items():
            ans.append(str(v)+' '+k)
        return ans

猜你喜欢

转载自www.cnblogs.com/zywscq/p/10569040.html