[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 that A[i] + B[j] + C[k] + D[l]
is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
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
在给定的4个数组中各取1个数字,使4个数的和为0,返回找到的次数。
如果用暴力搜索Bruce Force,那么Time: O(n^4),用时太长。
好的解法是用Hash map,建立一个map,将A,B中每两个数的和作为key,次数作为value写入map,然后查找C,D中每两个数和的相反数是否在map中,如果在,就把 map 中记录的次数累加到结果中。
Java:
public class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int res = 0;
HashMap<Integer,Integer> ab = new HashMap<Integer,Integer>(); for(int a:A){
for(int b:B){
ab.put(a+b,ab.getOrDefault(a+b,0) + 1);
}
}
for(int c:C){
for(int d:D){
int part2 = c + d;
int part1 = - part2;
res += ab.getOrDefault(part1,0);
}
}
return res;
}
}
Python:
class Solution(object):
def fourSumCount(self, A, B, C, D):
ans = 0
cnt = collections.defaultdict(int)
for a in A:
for b in B:
cnt[a + b] += 1
for c in C:
for d in D:
ans += cnt[-(c + d)]
return ans
Python: 这个写法牛逼到了只用两行就搞定了
class Solution(object):
def fourSumCount(self, A, B, C, D):
A_B_sum = collections.Counter(a+b for a in A for b in B)
return sum(A_B_sum[-c-d] for c in C for d in D)
C++:
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int res = 0;
unordered_map<int, int> m;
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < B.size(); ++j) {
++m[A[i] + B[j]];
}
}
for (int i = 0; i < C.size(); ++i) {
for (int j = 0; j < D.size(); ++j) {
int target = -1 * (C[i] + D[j]);
res += m[target];
}
}
return res;
}
};
类似题目:
All LeetCode Questions List 题目汇总
[LeetCode] 454. 4Sum II 四数之和II的更多相关文章
- LeetCode 18. 4Sum (四数之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- Leetcode(18)-四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
- [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] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 454 4Sum II 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...
- C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...
- 力扣 ——4Sum (四数之和)python 实现
题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...
随机推荐
- Codeforces J. Monotonic Renumeration(组合)
题目描述: You are given an array consisting of nmonotonic renumeration as an array b consisting of \(n\) ...
- pycharm下site-packages文件标记为红的问题;pycharm无法识别本地site-packages问题
当图示红框标记区域的文件夹颜色显示红色时,需要到FIle-setting里面设置好本地的运行环境,设置错误就会导致引用问题: 启动谷歌浏览器 from selenium import webdrive ...
- hexo博客微博图床失效解决办法
最近在v2ex上看到有人说微博图床开始限制外链了.当时我看了看我的博客,图片还好.第二天再去看的时候就挂了.评论里有人说改一个no-ferrer能解决. 记录一下操作方法. N:\blog\theme ...
- 更改DHCP服务器默认日志存储位置
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一种有效的IP 地址分配手段,已经广泛地应用于各种局域网管理.它能动态地向网络中每台计算机分配唯一 ...
- C# 验证控件的使用RequiredFieldValidator&CompareValidator
使用验证控件可以向服务器提交表单数据时验证表单内容,下面以RequiredFieldValidator和CompareValidator为例说明验证控件的用法 RequiredFieldValidat ...
- java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver at java.net.URLClassLoader.findClass(URLC ...
- Codeforces 1251E Voting
E2. Voting (Hard Version) 题意: 有n个人, 你想让他们都给你投票. 你可以选择花费pi收买第i个人, 或者如果有mi个人已经给你投票了, 那么第i个人会自动给你投票. 不妨 ...
- Java中多态
多态:把子类看成是父类,把实现类看成是接口,这样类就具有多种形态,称为多态. 在多态中访问成员变量,访问的是父类中的成员变量. 在多态中访问成员方法,先访问的是子类,看看子类有没有覆盖重写要访问的父类 ...
- circus 架构
转自官方文档:https://circus.readthedocs.io/en/latest/design/architecture/ Overall architecture Circus is c ...
- box-sizing 盒子模型
一.概念 ①外加模式: box-sizing: content-box 这是由 CSS2.1 规定的宽度高度行为.宽度和高度分别应用到元素的内容,在宽度和高度之外绘制元素的内边距,即宽和高不包括内边距 ...