博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode Hash Table知识点总结
阅读量:4221 次
发布时间:2019-05-26

本文共 1165 字,大约阅读时间需要 3 分钟。

不积跬步无以至千里,不写代码无法进行爱的供养

  • :求满足A[i]+B[j]+C[k]+D[l] = 0的 (i,j,k,l)个数,Medium

利用字典cnt,将A,B中各元素(笛卡尔积)的和进行分类计数。
将C,D中各元素(笛卡尔积)和的相反数在cnt中的值进行累加,即为答案。

import collectionsclass Solution(object):    def fourSumCount(self, A, B, C, D):        """        :type A: List[int]        :type B: List[int]        :type C: List[int]        :type D: List[int]        :rtype: int        """        ans = 0        cnt = collections.defaultdict(int)        for x in A:            for y in B:                cnt[x+y]+=1        for x in C:            for y in D:                ans += cnt[-(x+y)]        return ans
  • :计算每个域的访问次数,Easy
from collections import defaultdictclass Solution(object):    def subdomainVisits(self, cpdomains):        """        :type cpdomains: List[str]        :rtype: List[str]        """        maps = defaultdict(int)        for cpdomain in cpdomains:            cpdomain = cpdomain.split()            count = int(cpdomain[0])            strs = cpdomain[1].split('.')            for i in range(len(strs)):                s = '.'.join(strs[i:])                maps[s]+=count        ret = []        for a in maps:            ret += [str(maps[a])+' '+a]        return ret

转载地址:http://jbqmi.baihongyu.com/

你可能感兴趣的文章
jQuery核心--多库共存
查看>>
QTP Tutorial #23 – QTP Smart Object Identification, Sync Point, and Test Result Analysis
查看>>
第一章漫话自动化测试
查看>>
第二章:Selenium IDE应用实践
查看>>
第三章:Python基础
查看>>
正则表达式
查看>>
第五章 自动化测试模型
查看>>
Linux命令行与shell编程第3章基本的shell
查看>>
Linux命令行与shell编程第4章 更多的bash shell命令
查看>>
4 51 单片机最小系统
查看>>
6 51点亮第一个LED
查看>>
8 51 LED流水灯
查看>>
Multisim 14.0 搭建并仿真51单片机最小系统
查看>>
51 中断系统 外部中断0 外部中断1
查看>>
51 单片机 时间/计数器中断
查看>>
腾讯云本地还原mysql物理冷备
查看>>
算法图解 第1章 算法简介
查看>>
算法图解 第3章 递归
查看>>
Java反转整数
查看>>
解释 Zuul 的 zuul.strip-prefix 属性
查看>>