LeetCode 454. 4Sum II
454. 4Sum II
Description Submission Solutions
- Total Accepted: 8398
- Total Submissions: 18801
- Difficulty: Medium
- Contributors: Samuri
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
Subscribe to see which companies asked this question.
【题目分析】
给定四个长度相同的数组,在每个数组中取一个数字,在所有的组合中和为零的组合有多少个?
【思路】
把四个数组分为两组,每组包含两个数组。把其中一组中的任意两个值和存入hashmap中,然后在hashmap查找另外两个数组的值的组合。这其实是相当于转化为了一个two sum问题。
【java代码】
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<C.length; i++) {
for(int j=0; j<D.length; j++) {
int sum = C[i] + D[j];
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
int res=0;
for(int i=0; i<A.length; i++) {
for(int j=0; j<B.length; j++) {
res += map.getOrDefault(-1 * (A[i]+B[j]), 0);
}
}
return res;
}
代码中的map.getOrDefault(sum, 0)相比先在map中查找再取数的操作是比较高效的。
LeetCode 454. 4Sum II的更多相关文章
- [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 ...
- [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 (C++)
题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are su ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【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 ...
- 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), ...
- 454 4Sum II 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...
随机推荐
- Linux服务器access_log日志分析及配置详解(一)
nginx的log日志分为access log 和 error log 其中access log 记录了哪些用户,哪些页面以及用户浏览器.ip和其他的访问信息 error log 则是记录服务器错误日 ...
- A simple windows programm in c
A simple windows programm in c The following programm is a minimal windows program. It opens ...
- nginx配置访问图片路径(windows)
简介 Nginx(("engine x")由俄罗斯的程序设计师Igor Sysoev所开发)是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.PO ...
- 62. Unique Paths (走棋盘多少种不同的走法 动态规划)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- cdoj1580 简单图论问题
地址:http://acm.uestc.edu.cn/#/problem/show/1580 题目: 简单图论问题 Time Limit: 3000/1000MS (Java/Others) ...
- docker基本用法和命令
1.安装docker 检查有没有curl which curl 如果没有用以下命令可安装:sudo apt-get install curl 通过官方提供的脚本安装最新docker curl -sSL ...
- Python笔记 #02# Inner workings of lists
源:DataCamp datacamp 的 DAILY PRACTICE + 日常收集. List of lists Subset and conquer Slicing and dicing Li ...
- Python爬虫学习笔记之Centos下安装配置Mongodb3.6
在Centos6.9上安装Mongodb时候,遇到"No package mongodb-org available"这个报错. 经过查询后,在Centos6.9上需要针对Mong ...
- jqGrid('setSelection',rowid)报Cannot read property 'multiple' of undefined
项目组非要上jeeweb框架,用jqgrid+大量iframe做为前端框架,臃肿不堪. 今天上午,在进行选定操作jqGrid('setSelection',rowid)报Cannot read pro ...
- 一键安装lnmp(1)
#!/bin/bash#author:zhaocl#Software directory:$pathpath=`pwd`cd $path. $path/cacti.sh. $path/nginx.sh ...