leetcode-hard-array-454 4sum II-NO
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的更多相关文章
- LeetCode 454. 4Sum II
454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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), ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 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 ...
- 【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 ...
- 454 4Sum II 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...
随机推荐
- latex中文环境配置(针对北大模板,开题报告+中期答辩+毕业论文)
最近自己在忙着开题,中文环境真的是emm 以下只针对北大的毕业论文模板,至于其他的中文环境没有尝试 主要是用不同的latex编辑器会报不同的错误,当然我最后还是统一成了pdflatex,经过无数次尝试 ...
- 图片上传(3)(组件 -- 图片大小宽高限制)base64图片宽高读取
1.上传组件(可上传多个文件) <template> <div class="attachmentN"> <span class="btnS ...
- asp.net中的比较完美的验证码
要实现如图的效果的验证码,分以下步骤: 第一.布局好调用验证码的登录页面(命名:Login.aspx),注意:验证码位置可以是服务器控件 Image,也可以是html标签写的<img>,但 ...
- 进程、线程、协程的基本解析(python代码)
进程什么是进程?程序就是一堆放在磁盘上的代码,进程是一段程序的运行过程正规点说,进程一般由程序.数据集.进程控制块三部分组成 什么进程切换?进程切换是,一个正在运行的进程被中断,操作系统指定另一个进程 ...
- 第十章、os模块
目录 第十章.os模块 一.os模块 第十章.os模块 一.os模块 方法 详解 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirn ...
- 【异常】Could not find artifact com.wm.****:
1 详细异常 [ERROR] Failed to execute goal on project spark-etl: Could not resolve dependencies for proje ...
- 4.Shell 判断用户的参数
1.Shell 判断用户的参数 系统在执行mkdir命令时会判断用户输入的信息,即判断用户指定的文件夹名称是否已经存在,如果存在则提示报错:反之则自动创建. Shell脚本中的条件测试语法可以判断表达 ...
- View相关面试问题-事件分发面试问题讲解
为什么会有事件分发机制: 安卓上面的View是树形结构的,View可能会重叠在一起,当我们点击的地方有多个View都可以响应的时候,这个点击事件应该给谁呢?为了解决这一问题,就有了事件分发机制.用图来 ...
- Python:面向对象编程2
types.MethodType __slot__ @property, @xxx.setter Python的多重继承和MinIn 如何在class创建后,给实例绑定属性和方法? (动态绑定/定义 ...
- JavaScript对象原型
一.MDN上的解释(有点抽象) 基于原型的语言? JavaScript 常被描述为一种基于原型的语言 (prototype-based language)——每个对象拥有一个原型对象,对象以其原型为模 ...