4sumii
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的更多相关文章
- [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 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode哈希表
1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...
- leetcode454
public class Solution { public int FourSumCount(int[] A, int[] B, int[] C, int[] D) { var dic = new ...
- 454 4Sum II 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...
- [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通关:哈希表六连,这个还真有点简单
精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...
- 【力扣】454. 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
随机推荐
- Mybatis接口编程原理分析(三)
前面两篇博客Mybatis接口编程原理分析(一)和Mybatis接口编程原理分析(二)我们介绍了MapperProxyFactory.MapperProxy和MapperMethod的操作及源码分析, ...
- C语言--字符串和数字的相互转换
1.数字转换为字符串 sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出. sprintf 是个变参函数,定义如下: int sp ...
- ISLR系列:(2)分类 Logistic Regression & LDA & QDA & KNN
Classification 此博文是 An Introduction to Statistical Learning with Applications in R 的系列读书笔记,作为本人的一 ...
- viewpager循环滚动和自动轮播的问题
ViewPager是一个常用的android组件,不过通常我们使用ViewPager的时候不能实现左右无限循环滑动,在滑到边界的时候会看到一个不能翻页的动画,可能影响用户体验.此外,某些区域性的Vie ...
- FSM之SMC使用总结
FSM之SMC使用总结 Part1: Smc.jar state machine compiler usage Reference: http://smc.sourceforge.net/ ...
- 基于web的jfreechart的使用
这个模块的主要步骤就是: 前台通过struts调用后台,通过JFreeChart产生图片格式的图表,存储在某个位置,然后前台jsp再去调用图片. 来开工. JFreeChart的简介大家请百度. 首先 ...
- VectorDrawable与AnimatedVectorDrawable
VectorDrawable Android L开始提供了新的API VectorDrawable 可以使用SVG类型的资源,也就是矢量图.先来一个例子吧. <?xml version=&qu ...
- UE4 创建自己的角色
首先,需要在UE4中设置自己需要输入的按键,点击工具栏的Settings/ProjectSettings,找到input,在Bindings下添加自己需要的输入按键,我这里绑定了JumpBtn.Mov ...
- 【翻译】在Ext JS中创建特定主题的重写
Ext JS提供了大量的功能来使类的创建和处理变得简单,还提供了一系列的功能来扩展和重新现有的Javascript类.这意味着可以为类添加行为和创建属于自己的类,或者重写某些函数的行为.在本文,将展示 ...
- .NET(C#)连接各类数据库代码-集锦
1.C#连接连接Access 复制代码代码如下: using System.Data; using System.Data.OleDb; .. string strConnection=& ...