problem description:

  there is four number list named A,B,C,D; now you should out put the num of  tuples which statisfy A[i] +B[j]+C[k]+D[l] =0

i.e:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2] Output:
2 Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 解法:主要方法采用的是哈希表。将A和B列表中的每个值都进行两两相加,将得出的值作为哈希表的索引然后将哈希表该索引下的值加一。然后将C和D中的每个值两两相加,将所得出的值的相反数作为哈希表的索引,即可
得知相应的值有几种解法,然后进行累加。
python 实现方式:
class 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
"""
dicts = {}
for i in A:
for j in B:
sums = i+j
if sums not in dicts:
dicts[sums] = 1
else:
dicts[sums] += 1
out = 0
for k in C:
for l in D:
sums =-(k + l)
if sums in dicts:
out += dicts[sums]
return out

以上的方法虽有不错,但是还不是最精炼的python代码:

最精炼的python代码,用到了counter模块下的collection函数。它能够统计出相同数值的个数,本质上还是个哈希表

def fourSumCount(self, A, B, C, D):
AB = collections.Counter(a+b for a in A for b in B)
return sum(AB[-c-d] for c in C for d in D)

以上的第一个代码是个人想法,第二个精炼的代码参考自leetcode上stefanporchmann的代码

4sumii的更多相关文章

  1. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  2. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  3. LeetCode哈希表

    1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...

  4. leetcode454

    public class Solution { public int FourSumCount(int[] A, int[] B, int[] C, int[] D) { var dic = new ...

  5. 454 4Sum II 四数相加 II

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...

  6. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. LeetCode通关:哈希表六连,这个还真有点简单

    精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...

  8. 【力扣】454. 四数相加 II

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...

  9. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

随机推荐

  1. java实现:将一个数逆序输出

    前面我们用C语言实现过这个程序,其实java也一样的,很多步骤跟C差不多,但是有些接口和特性可能不同: import java.util.Scanner;//要使用scanner这个类,就需要导入一个 ...

  2. Java进阶(五)Junit测试

    我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在程序中只用到该函数的一小部分功能,并且经过调试可以确定,这一小部分功能是正确的.但是,我们同时应该确保每一个函数 ...

  3. 海量数据挖掘MMDS week1: Link Analysis - PageRank

    http://blog.csdn.net/pipisorry/article/details/48579435 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  4. 修改DrawerLayout 和toolbar 配合navigation的颜色

    大家都知道DrawerLayout 和toolbar 结合能出来高大上的效果. 使用到一个ActionBarDrawerToggle类. 那么怎么修改DrawerToggle的颜色呢,搜索了很多中文网 ...

  5. android图片加载库Glide

    什么是Glide? Glide是一个加载图片的库,作者是bumptech,它是在泰国举行的google 开发者论坛上google为我们介绍的,这个库被广泛的运用在google的开源项目中. Glide ...

  6. InfiniDB 修改一行的效率?

    InfiniDB引擎的DML速度比较慢,无论设置自动提交开关为关闭或开启,插入性能都很糟糕,但更新和删除的效率还可以,并且不支持truncate表操作. 删,改 效率高 插入,效率低(测试,在数据量稍 ...

  7. 《java入门第一季》之面向对象面试题(成员变量与局部变量的区别)

    /* 成员变量和局部变量的区别? A:在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同 成员变量:在堆内存 局部变量:在栈内存 C:生命周期不同 ...

  8. 【算法编程】基于Miller-Rabin的大素数测试

    基本原理: 费尔马小定理:如果p是一个素数,且0<a<p,则a^(p-1)%p=1.        利用费尔马小定理,对于给定的整数n,可以设计素数判定算法,通过计算d=a^(n-1)%n ...

  9. ITU-T G.1081 IPTV性能监测点 (Performance monitoring points for IPTV)

    ITU-T 建议书 G.1081 IPTV性能监测点 Performance monitoring points for IPTV Summary Successful deployment of I ...

  10. FFmpeg 2.1 试用(新版支持HEVC,VP9)

    前两天帮一位老师转码图像的时候,无意间发现新版FFmpeg竟然支持了下一代编码标准HEVC,以及Google提出的下一代编码标准VP9.真心没想到FFmpeg对下一代的编码标准支持的是如此之快.我还以 ...