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 ...
随机推荐
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
原文出处: oschina (API:http://ifeve.com/category/framework/guava-2/ JAR DOC Source 链接:http://pan.baidu.c ...
- 12、Libgdx的图像之全屏和垂直同步
(官网:www.libgdx.cn) 检测当前设置 判断是否设置全屏,可以通过如下方式: boolean fullscreen = Gdx.graphics.isFullscreen(); 设置全屏和 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(七)
因为到目前为止我都是在iOS模拟器中测试,但即便如此,也觉得按住手臂旋转时,手臂转动起来比较费劲,很难停止在玩家期望的位置上.因为手臂完全通过物理引擎的计算来移动,它有自身的惯性影响,所以很难控制. ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- React Native之样式
样式 React Native 不实现 CSS,而是依赖于 JavaScript 来为你的应用程序设置样式.这是一个有争议的决定,你可以阅读那些幻灯片,了解背后的基本原理. 声明样式 在 React ...
- 学习pthreads,使用互斥量进行同步
在进行多线程编程时,我们总会遇到全局变量和数据结构的问题,这是多线程之间进行通信的问题.如果多个线程同时读写一个全局变量,那么会造成竞争或者出错.为了解决这一问题,我们需要对全局数据进行,使用互斥量实 ...
- 基于VB中WINSOCK控件的网上象棋系统的实现
本文发表在<微型机与应用>杂志2001年第3期. 基于VB中WINSOCK控件的网上象棋系统的实现 马根峰1 , 孙艳2 , 王平1 (1.重庆邮电学院自动化学院,重庆,40006 ...
- PO订单审批拒绝API
DECLARE l_return_status VARCHAR2(1); l_return_code VARCHAR2(1000); l_exception_msg VARCHAR2(4000); l ...
- 内存数据网格hazelcast的一些机制原理
hazelcast作为一个内存数据网格工具,还算比较优秀,听说有Apache顶级项目使用它,值得研究下,使用文档可以直接看官方文档,但机制原理相关的资料基本没有,本人硬撸源码写的一些东西,跟大家分享一 ...
- 安卓笔记--Style的继承
比如想要重写一个对话框的style <style name="Theme_dialog" parent="@android:style/Theme.Dialog&q ...