题目

给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

0 <= i, j, k, l < n

nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

示例 1:

输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]

输出:2

解释:

两个元组如下:

  1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
  2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/4sum-ii

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Java实现(双for + HashMap)

package LC.hash;

import java.util.HashMap;
import java.util.Map; public class LC454 {
public static void main(String[] args) {
int[] nums1 = {1, 2};
int[] nums2 = {-2, -1};
int[] nums3 = {-1, 2};
int[] nums4 = {0, 2};
System.out.println(fourSumCount(nums1, nums2, nums3, nums4));
} public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
int res = 0;
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
int sumAB = A[i] + B[j];
if (map.containsKey(sumAB))
map.put(sumAB, map.get(sumAB) + 1);
else map.put(sumAB, 1);
}
} for (int i = 0; i < C.length; i++) {
for (int j = 0; j < D.length; j++) {
int sumCD = -(C[i] + D[j]);
if (map.containsKey(sumCD))
res += map.get(sumCD);
}
}
return res;
}
}

Python实现

class Solution:
def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:
countAB = collections.Counter(u + v for u in A for v in B)
ans = 0
for u in C:
for v in D:
if -u -v in countAB:
ans = ans + countAB[-u -v]
return ans

总结

看到形如:A+B....+N = 0 的式子,

要转换为(A+...T)=-((T+1)...+N)再计算,这个T的分割点一般是一半,特殊情况下需要自行判断。

定T是解题的关键。

就这这个醋包了这顿饺子

import java.util.HashMap;

class Solution {
private static class Node {
int value;
int count;
Node next; public Node(int value) {
this.value = value;
this.count = 1;
} public Node(int value, Node next) {
this.value = value;
this.count = 1;
this.next = next;
}
} private static class Map { Node[] table; public Map(int initalCapacity) {
if (initalCapacity < 16) {
initalCapacity = 16;
} else {
initalCapacity = Integer.highestOneBit(initalCapacity - 1) << 1;
}
table = new Node[initalCapacity];
} // 拷贝的HashMap的hash方法
private int hash(int value) {
if (value < 0) {
value = -value;
}
int h;
return (value == 0) ? 0 : (h = value) ^ (h >>> 16);
} public void put(int value) {
int tableIndex = hash(value) & table.length - 1;
Node head = table[tableIndex];
if (head == null) {
table[tableIndex] = new Node(value);
return;
}
Node cur = head;
while (cur != null) {
if (cur.value == value) {
cur.count++;
return;
}
cur = cur.next;
} // 头插法
table[tableIndex] = new Node(value, head);
} public int getCount(int value) {
int tableIndex = hash(value) & table.length - 1;
Node head = table[tableIndex];
if (head == null) {
return 0;
}
Node cur = head;
while (cur != null) {
if (cur.value == value) {
return cur.count;
}
cur = cur.next;
}
return 0;
}
} public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { // 避免扩容, 初始化一个最大初始容量
Map abMap = new Map(A.length * B.length); for (int a : A) {
for (int b : B) {
abMap.put(a + b);
}
} int res = 0;
for (int c : C) {
for (int d : D) {
res += abMap.getCount(-c - d);
}
}
return res;
} }

LC-454的更多相关文章

  1. 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 ...

  2. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

  3. “LC.exe”错误

    错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...

  4. 解决VS下“LC.exe已退出,代码为-1”问题

    今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...

  5. 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析

    许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...

  6. Lc.exe已退出,代码为-1

    编译项目,出现提示"Lc.exe已退出,代码为-1" .   解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...

  7. "LC.exe" exited with code -1 错误

    当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可

  8. LC.exe exited with code -1

    昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...

  9. vs2012编译出错“LC.exe”已退出解决方法

    “LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.

  10. TT付款方式、前TT和后TT、LC信用证+TT付款方式

    TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...

随机推荐

  1. jq集成月份插件(不要年月日,只要年月)

    最近项目中报表统计需要按照月份进行统计,以前用的jq-ui的插件,里面集成的是年月日,修改了好久,觉得太过麻烦,就换了个思路,由于赶时间,所以就度娘了,找到了一个很简洁又简单的Demo,但是发现和我的 ...

  2. Net6 Configuration & Options 源码分析 Part1

    Net6 Configuration & Options 源码分析 Part1 在Net6中配置系统一共由两个部分组成Options 模型与配置系统.它们是两个完全独立的系统. 第一部分主要记 ...

  3. 如何写好B端产品的技术方案?

    B端产品为企业提供协同办公的工具,帮助企业解决某类经营管理问题,核心价值在于为企业增加收入.降本提效.管控风险,企业级SaaS产品也是B端产品中的一类. B端产品有以下特点: ​客户是一个群体:B端产 ...

  4. ENVI提取水系并进行生态敏感性分析

    4 具体步骤 4.1 DEM数据拼接 (1)打开ENVI软件,选择[File][Open],添加文件夹DEM数据中的影像,操作如图4.1.1所示,结果如图4.1.2. 图4.1.1 添加DEM数据影像 ...

  5. 分享一些阅读Java相关框架源码的经验

    昨天和部分网友进行了线上的交流,交流中有不少网友提到阅读源码比较难,不知道怎么上手.胖哥分享了一些个人经验,这里总结一下. 阅读源码实际上是Debug源码 其实所谓的阅读源码并不是单纯的阅读,而是调试 ...

  6. 合并两个以单链表形式表示的关于x的多项式(基于c语言)

    只写函数内部的,不懂得可以看前面一篇文章对链表的实现: pLinklist addBothLinklist(Linklist* first,Linklist* second){ Linklist *n ...

  7. ActiveX 是什么,和IE什么关系

    在推出25年多以后,IE浏览器终于要退役了. 据外媒报道,微软决定自2022年6月15日起,让IE浏览器彻底退出互联网舞台,并全面改用Microsoft Edge浏览器. 关于IE的历史可以参考这篇文 ...

  8. flask 数据库一节笔记

    笔记一:os.path的用法:1. os.path.dirname(__file__)   返回当前脚本的执行路径,__file__为固定参数2. os.path.abspath(file)     ...

  9. Mybatis的xml配置(mybatis-config.xml)精简笔记

    老规矩,看着官方文档学 首先,我们需要知道的是,在MyBatis 的xml配置文件中,这些影响 MyBatis 行为的属性之间的设置是有先后顺序的.配置的先后顺序依照properties, setti ...

  10. 深入理解RPC—序列化

    深入理解RPC-序列化 xiaofang233 2020-09-18 16:38:22 1024 收藏 6分类专栏: 分布式服务版权为什么需要序列化?首先,我们得知道什么是序列化与反序列化. 我们先回 ...