mycode   过不了。。。我也不知道为什么。。。

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
"""
def deal(i,j,k,l):
if [i,j,k,l] not in self.record:
self.record.append([i,j,k,l])
else:
return
#print(self.res,i,j,k,l)
if i < I and j < J and k < K and l < L:
if A[i] + B[j] + C[k] + D[l] == 0:
self.res += 1
print(self.res,i,j,k,l)
elif A[i] + B[j] + C[k] + D[l] < 0:
deal(i,j,k,l+1)
deal(i,j,k+1,l)
deal(i,j+1,k,l)
deal(i+1,j,k,l)
else:
return
else:
return A,B,C,D = sorted(A),sorted(B),sorted(C),sorted(D)
i,j,k,l = 0,0,0,0
I,J,K,L = len(A),len(B),len(C),len(D)
self.res = 0
self.record = []
deal(i,j,k,l)
return self.res

参考

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
""" from collections import Counter
dicA , dicB ,dicC ,dicD = Counter(A), Counter(B), Counter(C), Counter(D)
res = 0
dic = {}
for a , a_nember in dicA.items():
for b , b_nember in dicB.items():
dic[a+b] = dic.get(a+b,0) + a_nember * b_nember
#例如2+(-1)=1,而A中2的数量*B中-2的数量+B中2的数量*A中-2的数量就等于A和B中元素和为1的组合数
for c, c_nember in dicC.items():
for d, d_nember in dicD.items():
res += dic.get(-c-d,0) * c_nember * d_nember
return res

leetcode-hard-array-454 4sum II-NO的更多相关文章

  1. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

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

  3. LC 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 ...

  4. [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 ...

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

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

  6. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

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

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

  8. LeetCode 454. 4Sum II (C++)

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

  9. 【LeetCode】454 4Sum II

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

  10. 454 4Sum II 四数相加 II

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

随机推荐

  1. HBuilderX 打包

    新建 - 云打包 (密钥 密码看不到 - 回车)    (  ) BlueStacks蓝叠 模拟器看效果

  2. vue 节流

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Tomcat----服务运行的容器

    在介绍Tomcat之前,我们先介绍一个概念Servlet. Servlet是一个运行在WEB服务器上的小的Java程序,用来接收和响应从客户端发送过来的请求,通常使用HTTP协议.从下图可以看出Ser ...

  4. 5.java动态代理、反射

    1.java动态代理.反射(IDEA导入JUnit4) 1.1.反射 通过反射的方式可以获取class对象中的属性.方法.构造函数等 1.2.反射代码 import java.io.Serializa ...

  5. BZOJ 1002 生成树计数&高精度

    给你定义一种特殊的图 这种图总共有n个节点 假设编号为0~n-1 首先1~n-1排成环形 每个点与相邻的两个点有边 其次这n-1个节点每个和0节点有一条边 每次询问你一个n 要回到当前n节点的特殊图有 ...

  6. std::map 的swap错用

    map<int, shared_ptr<int>>map_test; shared_ptr<); map_test[] = tmp_1; shared_ptr<); ...

  7. 需要以管理员的身份运行程序(winform)

    1.添加应用程序清单文件(app.manifest) 2.打开app.manifest,将<requestedExecutionLevel level="asInvoker" ...

  8. VMware中linux虚拟机无法全屏显示的解决方法(1080P分辨率调节)

    安装了linux(这里以Ubuntu为例)后,发现无法全屏,如下图 即使在设置里面也找不到适合我的显示器的分辨率(我的显示器为1080P) 网上很多人都说是因为没装显卡驱动,其实不是这样的.要解决这个 ...

  9. Module parse failed: Export 'instance' is not defined (35:19)

    Module parse failed: Export 'instance' is not defined (35:19) 使用npm出现的这错误,用yarn就可以了 这种情况rm node_modu ...

  10. SpringCloud之网关 Gateway(五)

    前面我们在聊服务网关Zuul的时候提到了Gateway,那么Zuul和Gateway都是服务网关,这两个有什么区别呢? 1. Zuul和Gateway的恩怨情仇 1.1 背景 Zuul是Netflix ...