LeetCode Explanation Chapter 454. Addition of Four Numbers II
Article directory
Question description
Problem solving ideas
The hash table records all combined evaluations and quantities of the first two arrays, and then combines the evaluations of the last two arrays. If the value exists in the hash table, the quantity corresponding to the value is accumulated. After completing the step task, the accumulated value is returned
Solution code
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (res int) {
m := make(map[int]int)
l := len(nums2) - 1
for i := len(nums1) - 1; i >= 0; i-- {
for j := l; j >= 0; j-- {
m[nums1[i] + nums2[j]]++
}
}
l = len(nums4) - 1
for i := len(nums3) - 1; i >= 0; i-- {
for j := l; j >= 0; j-- {
res+=m[-nums3[i]-nums4[j]]
}
}
return
}