50
50
51
51
<!-- 这里可写通用的实现逻辑 -->
52
52
53
- ** 方法一:分组 + 哈希表**
53
+ ** 方法一:哈希表**
54
54
55
- 时间复杂度为 $O(n^2)$,空间复杂度为 $O(n^2)$,其中 $n$ 是数组的长度。
55
+ 我们可以将数组 $nums1$ 和 $nums2$ 中的元素 $a$ 和 $b$ 相加,将所有可能的和存储在哈希表 $cnt$ 中,其中键为两数之和,值为两数之和出现的次数。
56
+
57
+ 然后我们遍历数组 $nums3$ 和 $nums4$ 中的元素 $c$ 和 $d$,令 $c+d$ 为目标值,那么答案即为 $cnt[ -(c+d)] $ 的累加和。
58
+
59
+ 时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$,其中 $n$ 是数组的长度。
56
60
57
61
<!-- tabs:start -->
58
62
@@ -65,15 +69,8 @@ class Solution:
65
69
def fourSumCount (
66
70
self , nums1 : List[int ], nums2 : List[int ], nums3 : List[int ], nums4 : List[int ]
67
71
) -> int :
68
- counter = Counter()
69
- for a in nums1:
70
- for b in nums2:
71
- counter[a + b] += 1
72
- ans = 0
73
- for c in nums3:
74
- for d in nums4:
75
- ans += counter[- (c + d)]
76
- return ans
72
+ cnt = Counter(a + b for a in nums1 for b in nums2)
73
+ return sum (cnt[- (c + d)] for c in nums3 for d in nums4)
77
74
```
78
75
79
76
### ** Java**
@@ -83,16 +80,16 @@ class Solution:
83
80
``` java
84
81
class Solution {
85
82
public int fourSumCount (int [] nums1 , int [] nums2 , int [] nums3 , int [] nums4 ) {
86
- Map<Integer , Integer > counter = new HashMap<> ();
83
+ Map<Integer , Integer > cnt = new HashMap<> ();
87
84
for (int a : nums1) {
88
85
for (int b : nums2) {
89
- counter . put (a + b, counter . getOrDefault(a + b, 0 ) + 1 );
86
+ cnt . merge (a + b, 1 , Integer :: sum );
90
87
}
91
88
}
92
89
int ans = 0 ;
93
90
for (int c : nums3) {
94
91
for (int d : nums4) {
95
- ans += counter . getOrDefault(- (c + d), 0 );
92
+ ans += cnt . getOrDefault(- (c + d), 0 );
96
93
}
97
94
}
98
95
return ans;
@@ -106,14 +103,18 @@ class Solution {
106
103
class Solution {
107
104
public:
108
105
int fourSumCount(vector<int >& nums1, vector<int >& nums2, vector<int >& nums3, vector<int >& nums4) {
109
- unordered_map<int, int> counter;
110
- for (int a : nums1)
111
- for (int b : nums2)
112
- ++counter[ a + b] ;
106
+ unordered_map<int, int> cnt;
107
+ for (int a : nums1) {
108
+ for (int b : nums2) {
109
+ ++cnt[ a + b] ;
110
+ }
111
+ }
113
112
int ans = 0;
114
- for (int c : nums3)
115
- for (int d : nums4)
116
- ans += counter[ -(c + d)] ;
113
+ for (int c : nums3) {
114
+ for (int d : nums4) {
115
+ ans += cnt[ -(c + d)] ;
116
+ }
117
+ }
117
118
return ans;
118
119
}
119
120
};
@@ -122,20 +123,46 @@ public:
122
123
### **Go**
123
124
124
125
```go
125
- func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
126
- counter := make( map[int]int)
126
+ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) {
127
+ cnt := map[int]int{}
127
128
for _, a := range nums1 {
128
129
for _, b := range nums2 {
129
- counter [a+b]++
130
+ cnt [a+b]++
130
131
}
131
132
}
132
- ans := 0
133
133
for _, c := range nums3 {
134
134
for _, d := range nums4 {
135
- ans += counter [-(c + d)]
135
+ ans += cnt [-(c + d)]
136
136
}
137
137
}
138
- return ans
138
+ return
139
+ }
140
+ ```
141
+
142
+ ### ** TypeScript**
143
+
144
+ ``` ts
145
+ function fourSumCount(
146
+ nums1 : number [],
147
+ nums2 : number [],
148
+ nums3 : number [],
149
+ nums4 : number [],
150
+ ): number {
151
+ const cnt: Map <number , number > = new Map ();
152
+ for (const a of nums1 ) {
153
+ for (const b of nums2 ) {
154
+ const x = a + b ;
155
+ cnt .set (x , (cnt .get (x ) || 0 ) + 1 );
156
+ }
157
+ }
158
+ let ans = 0 ;
159
+ for (const c of nums3 ) {
160
+ for (const d of nums4 ) {
161
+ const x = c + d ;
162
+ ans += cnt .get (- x ) || 0 ;
163
+ }
164
+ }
165
+ return ans ;
139
166
}
140
167
```
141
168
0 commit comments