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, ...
随机推荐
- 《python解释器源码剖析》第8章--python的字节码与pyc文件
8.0 序 我们日常会写各种各样的python脚本,在运行的时候只需要输入python xxx.py程序就执行了.那么问题就来了,一个py文件是如何被python变成一系列的机器指令并执行的呢? 8. ...
- string类的总结
一.string类头文件:#include <string>;using namespace std; 二.string类方法: 1.获取string的字符串长度:size(),size返 ...
- three.js之元素周期表
<html><head> <title>three.js css3d - periodic table</title> <meta charset ...
- Perl环境安装
在我们开始学习 Perl 语言前,我们需要先安装 Perl 的执行环境. Perl 可以在以下平台下运行: Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, Sun ...
- shell脚本基础和grep文本处理工具企业应用3
文本处理工具: linux上文本处理三剑客 grep,egrep,fgrep:文本过滤工具(模式:pattern)工具 grep:默认支持的是基本正则表达式: ...
- 1.opencv_画图
#导入工具包 import numpy as np import cv2 import matplotlib.pyplot as plt # 定义显示图片 def show(image): plt.i ...
- CNN for NLP
卷积神经网络在自然语言处理任务中的应用.参考链接:Understanding Convolutional Neural Networks for NLP(2015.11) Instead of ima ...
- 转 SQL连接查询语句(内、外、交叉和合并查询)
转 http://blog.csdn.net/u010011371/article/details/50596535 1.内连接 (INNER JOIN) 内连接也称自然连接,它是根据两个或多个表中的 ...
- org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
报错信息如下: org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters ar ...
- 【洛谷P2387】魔法森林
题目大意:给定一个 N 个点,M 条边的无向图,边有两个边权 a, b,求从 1 号节点到 N 号节点路径的两个权值和的最大值最小是多少. 题解: 对于有两个属性的结构的最优化问题,可以考虑先按照其中 ...