Golang Leetcode 454. 4Sum II.go

时间:2022-06-19
本文章向大家介绍Golang Leetcode 454. 4Sum II.go,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89143799

思路

和两个数相加的题目比较类似的解法

code

func fourSumCount(A []int, B []int, C []int, D []int) int {
	maps := make(map[int]int)
	for i := 0; i < len(A); i++ {
		for j := 0; j < len(B); j++ {
			maps[A[i]+B[j]]++
		}
	}
	ret := 0
	for i := 0; i < len(C); i++ {
		for j := 0; j < len(D); j++ {
			ret += maps[-1*(C[i]+D[j])]
		}
	}
	return ret
}