454. 4Sum 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

此题本来以为是按照four sum的思路来做的,即先将四个数组排好序,然后两个for循环分别循环A,B数组,再用两个指针分别指向C的low和D的high,但是发现得到的数字比期待的数字少,测试用例如下:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }

Input:[0,1,-1] [-1,1,0] [0,0,1] [-1,1,1]

Output:12

Expected:17

后来思考了一下,发现这么做确实不可以,后两个数组每次都不能遍历完所有可能结果,由此可以推断出来,两个数组里面想用two pointer来全部遍历是无法做到的,除非刚好一个数组的最大值等于或者小于另一个数组的最小值。

代码如下:

p.p1 { margin: 0.0px 0.0px 2.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #454545 }

public class Solution {

public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {

int n =A.length;

Arrays.sort(A);

Arrays.sort(B);

Arrays.sort(C);

Arrays.sort(D);

int count = 0;

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

int low = 0,high = n-1;

while(low<n&&high>=0){

int sum = A[i]+B[j]+C[low]+D[high];

if(sum==0){

count++;

low++;

}else if(sum<0){

low++;

}else{

high--;

}

}

}

}

return count;

}

}

看了大神的做法,用了hashmap来做,key保存两个数组的和,value保存出现该和的个数,然后如果另外两个数组的和等于这个hashmap的-key,就加上value的值。

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 }

public class Solution {

public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {

Map<Integer,Integer> map = new HashMap<Integer,Integer>();

int n = A.length;

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

map.put(A[i]+B[j],map.getOrDefault(A[i]+B[j],0)+1);

}

}

int count = 0;

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

count+=map.getOrDefault(-C[i]-D[j],0);

}

}

return count;

}

}

这里面Object getOrDefault(Object key, Object defaultValue),表示返回map键对应的map值,如果不存在map键,返回默认值。

时间: 2024-08-18 07:03:17

454. 4Sum II的相关文章

LeetCode 454. 4Sum II

454. 4Sum II Add to List 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[

[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 r

【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 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 t

18 &amp; 454. 4Sum I &amp; II

18. 4Sum I (32lines) Given a integer array and target, find all unique quadruplets sum is target. Solution(O(N^3)): Similar as 3sum problem, add one more loop and be careful about the duplicates. 1 public class Solution { 2 public List<List<Integer&

Leetcode: 4Sum 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 r

[Swift]LeetCode454. 四数相加 II | 4Sum 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 ra

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea