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-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0401_0500.s0454_4sum_ii;
// #Medium #Top_Interview_Questions #Array #Hash_Table
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;
}
}