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, ...
随机推荐
- spring ioc aop 理解
OC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果 ...
- Java面向对象(一)
面向对象(Object Oriented) 面向过程:事物比较简单.将问题分解为若干个步骤.按照步骤依次执行.面向对象:事物比较复杂.在解决面向对象的过程中,最后的执行部分还是面向过程方式,面向过程和 ...
- HTML 做圆形头像
Click me <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 【2017-03-28】JS基础、windows对象、history对象、location对象
一.JS基础 JS - javaScript 1.js功能: 1).进行数据的运算.2).控制浏览器的一些功能.3).控制元素(属性.内容.样式) js引用位置: 可以放在html页的任意位置. 推荐 ...
- IntelliJ IDEA导包快捷键
IntelliJ IDEA导包快捷键 Alt+Enter
- MySQL数据优化总结-查询备忘录
一.优化分类 二.测试数据样例 参考mysql官方的sakina数据库. 三.使用mysql慢查询日志对有效率问题的sql进行监控 第一个,开启慢查询日志.第二个,慢查询日志存储位置.第三个,没有使用 ...
- mongodb cursor用法
为了营造大批量数据,我们可以这样写javascript脚本 for (var i=1;i<=10000;i++) { if(i%2==1) { db.cursortest.insert({_id ...
- 01- ES6、jquery源码、node、webpack
1.课程介绍 小马哥blog:https://www.cnblogs.com/majj/ 前端学习路径:https://www.processon.com/view/link/5d3a5947e4b0 ...
- vue学习时遇到的问题(一)
1.vue的异步组件,require()方法 作用是:在需要使用的时候,从 根目录/components/HelloWorld.vue 加载组件 import Vue from 'vue' impor ...
- jquery scroll()方法 语法
jquery scroll()方法 语法 作用:当用户滚动指定的元素时,会发生 scroll 事件.scroll 事件适用于所有可滚动的元素和 window 对象(浏览器窗口).scroll() 方法 ...