Skip to content

Commit be5c774

Browse files
committed
feat: add solutions to lc problem: No.0454
No.0454.4Sum II
1 parent 7509cc3 commit be5c774

File tree

6 files changed

+183
-16
lines changed

6 files changed

+183
-16
lines changed

solution/0400-0499/0454.4Sum II/README.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ D = [ 0, 2]
2828
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
2929
</pre>
3030

31-
3231
## 解法
3332

3433
<!-- 这里可写通用的实现逻辑 -->
@@ -40,15 +39,80 @@ D = [ 0, 2]
4039
<!-- 这里可写当前语言的特殊实现逻辑 -->
4140

4241
```python
43-
42+
class Solution:
43+
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
44+
counter = Counter()
45+
for a in nums1:
46+
for b in nums2:
47+
counter[a + b] += 1
48+
ans = 0
49+
for c in nums3:
50+
for d in nums4:
51+
ans += counter[-(c + d)]
52+
return ans
4453
```
4554

4655
### **Java**
4756

4857
<!-- 这里可写当前语言的特殊实现逻辑 -->
4958

5059
```java
60+
class Solution {
61+
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
62+
Map<Integer, Integer> counter = new HashMap<>();
63+
for (int a : nums1) {
64+
for (int b : nums2) {
65+
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
66+
}
67+
}
68+
int ans = 0;
69+
for (int c : nums3) {
70+
for (int d : nums4) {
71+
ans += counter.getOrDefault(-(c + d), 0);
72+
}
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
85+
unordered_map<int, int> counter;
86+
for (int a : nums1)
87+
for (int b : nums2)
88+
++counter[a + b];
89+
int ans = 0;
90+
for (int c : nums3)
91+
for (int d : nums4)
92+
ans += counter[-(c + d)];
93+
return ans;
94+
}
95+
};
96+
```
5197
98+
### **Go**
99+
100+
```go
101+
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
102+
counter := make(map[int]int)
103+
for _, a := range nums1 {
104+
for _, b := range nums2 {
105+
counter[a+b]++
106+
}
107+
}
108+
ans := 0
109+
for _, c := range nums3 {
110+
for _, d := range nums4 {
111+
ans += counter[-(c + d)]
112+
}
113+
}
114+
return ans
115+
}
52116
```
53117

54118
### **...**

solution/0400-0499/0454.4Sum II/README_EN.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,85 @@ The two tuples are:
4242
<li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li>
4343
</ul>
4444

45-
4645
## Solutions
4746

4847
<!-- tabs:start -->
4948

5049
### **Python3**
5150

5251
```python
53-
52+
class Solution:
53+
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
54+
counter = Counter()
55+
for a in nums1:
56+
for b in nums2:
57+
counter[a + b] += 1
58+
ans = 0
59+
for c in nums3:
60+
for d in nums4:
61+
ans += counter[-(c + d)]
62+
return ans
5463
```
5564

5665
### **Java**
5766

5867
```java
68+
class Solution {
69+
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
70+
Map<Integer, Integer> counter = new HashMap<>();
71+
for (int a : nums1) {
72+
for (int b : nums2) {
73+
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
74+
}
75+
}
76+
int ans = 0;
77+
for (int c : nums3) {
78+
for (int d : nums4) {
79+
ans += counter.getOrDefault(-(c + d), 0);
80+
}
81+
}
82+
return ans;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
93+
unordered_map<int, int> counter;
94+
for (int a : nums1)
95+
for (int b : nums2)
96+
++counter[a + b];
97+
int ans = 0;
98+
for (int c : nums3)
99+
for (int d : nums4)
100+
ans += counter[-(c + d)];
101+
return ans;
102+
}
103+
};
104+
```
59105
106+
### **Go**
107+
108+
```go
109+
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
110+
counter := make(map[int]int)
111+
for _, a := range nums1 {
112+
for _, b := range nums2 {
113+
counter[a+b]++
114+
}
115+
}
116+
ans := 0
117+
for _, c := range nums3 {
118+
for _, d := range nums4 {
119+
ans += counter[-(c + d)]
120+
}
121+
}
122+
return ans
123+
}
60124
```
61125

62126
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
4+
unordered_map<int, int> counter;
5+
for (int a : nums1)
6+
for (int b : nums2)
7+
++counter[a + b];
8+
int ans = 0;
9+
for (int c : nums3)
10+
for (int d : nums4)
11+
ans += counter[-(c + d)];
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
2+
counter := make(map[int]int)
3+
for _, a := range nums1 {
4+
for _, b := range nums2 {
5+
counter[a+b]++
6+
}
7+
}
8+
ans := 0
9+
for _, c := range nums3 {
10+
for _, d := range nums4 {
11+
ans += counter[-(c + d)]
12+
}
13+
}
14+
return ans
15+
}
+11-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
2-
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
3-
Map<Integer, Integer> map = new HashMap<>();
4-
for (int a : A) {
5-
for (int b : B) {
6-
int key = a + b;
7-
map.put(key, map.getOrDefault(key, 0) + 1);
2+
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
3+
Map<Integer, Integer> counter = new HashMap<>();
4+
for (int a : nums1) {
5+
for (int b : nums2) {
6+
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
87
}
98
}
10-
int res = 0;
11-
for (int c : C) {
12-
for (int d : D) {
13-
res += map.getOrDefault(-(c + d), 0);
9+
int ans = 0;
10+
for (int c : nums3) {
11+
for (int d : nums4) {
12+
ans += counter.getOrDefault(-(c + d), 0);
1413
}
1514
}
16-
return res;
15+
return ans;
1716
}
18-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
3+
counter = Counter()
4+
for a in nums1:
5+
for b in nums2:
6+
counter[a + b] += 1
7+
ans = 0
8+
for c in nums3:
9+
for d in nums4:
10+
ans += counter[-(c + d)]
11+
return ans

0 commit comments

Comments
 (0)