Golang Leetcode 454. 4Sum II.go

思路

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

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
}

更多内容请移步我的repo:https://github.com/anakin/golang-leetcode

你可能感兴趣的:(leetcode-golang,golang,leetcode,算法)