g0401_0500.s0454_4sum_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0401_0500.s0454_4sum_ii;
// #Medium #Top_Interview_Questions #Array #Hash_Table
// #2022_07_18_Time_133_ms_(95.19%)_Space_42.4_MB_(88.53%)
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int count = 0;
Map map = new HashMap<>();
for (int k : nums3) {
for (int i : nums4) {
int sum = k + i;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for (int k : nums1) {
for (int i : nums2) {
int m = -(k + i);
count += map.getOrDefault(m, 0);
}
}
return count;
}
}