LeetCode 454. 4Sum II
2022.09.11
問題
https://leetcode.com/problems/4sum-ii/
typescript
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
const map = new Map<number, number>()
let ans = 0
for (const num1 of nums1) {
for (const num2 of nums2) {
const sum = num1 + num2
map.set(sum, (map.get(sum) || 0) + 1)
}
}
for (const num3 of nums3) {
for (const num4 of nums4) {
const target = - num3 - num4
ans += map.get(target) || 0
}
}
return ans
};
- num1, num2と num3, num4に分ける
- 全探索