Merge pull request #2563 from Yan0613/master

Update 0454.四数相加II.md,修改四数相加IIGo版本的解答代码
This commit is contained in:
程序员Carl
2024-06-18 10:15:57 +08:00
committed by GitHub

View File

@ -186,20 +186,26 @@ class Solution:
```go
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
m := make(map[int]int) //key:a+b的数值value:a+b数值出现的次数
m := make(map[int]int)
count := 0
// 遍历nums1和nums2数组统计两个数组元素之和和出现的次数放到map中
// 构建nums1和nums2的和的map
for _, v1 := range nums1 {
for _, v2 := range nums2 {
m[v1+v2]++
}
}
// 遍历nums3和nums4数组找到如果 0-(c+d) 在map中出现过的话就把map中key对应的value也就是出现次数统计出来
// 遍历nums3和nums4检查-c-d是否在map中
for _, v3 := range nums3 {
for _, v4 := range nums4 {
count += m[-v3-v4]
sum := -v3 - v4
if countVal, ok := m[sum]; ok {
count += countVal
}
}
}
return count
}
```