Skip to content

Commit 72edb0e

Browse files
committed
feat: add solutions to lc problem: No.0454
No.0454.4Sum II
1 parent 33a2a02 commit 72edb0e

File tree

13 files changed

+153
-84
lines changed

13 files changed

+153
-84
lines changed

solution/0200-0299/0225.Implement Stack using Queues/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class MyStack:
2-
32
def __init__(self):
43
self.q1 = deque()
54
self.q2 = deque()

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

+54-27
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
**方法一:分组 + 哈希表**
53+
**方法一:哈希表**
5454

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$ 是数组的长度。
5660

5761
<!-- tabs:start -->
5862

@@ -65,15 +69,8 @@ class Solution:
6569
def fourSumCount(
6670
self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]
6771
) -> 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)
7774
```
7875

7976
### **Java**
@@ -83,16 +80,16 @@ class Solution:
8380
```java
8481
class Solution {
8582
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<>();
8784
for (int a : nums1) {
8885
for (int b : nums2) {
89-
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
86+
cnt.merge(a + b, 1, Integer::sum);
9087
}
9188
}
9289
int ans = 0;
9390
for (int c : nums3) {
9491
for (int d : nums4) {
95-
ans += counter.getOrDefault(-(c + d), 0);
92+
ans += cnt.getOrDefault(-(c + d), 0);
9693
}
9794
}
9895
return ans;
@@ -106,14 +103,18 @@ class Solution {
106103
class Solution {
107104
public:
108105
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+
}
113112
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+
}
117118
return ans;
118119
}
119120
};
@@ -122,20 +123,46 @@ public:
122123
### **Go**
123124
124125
```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{}
127128
for _, a := range nums1 {
128129
for _, b := range nums2 {
129-
counter[a+b]++
130+
cnt[a+b]++
130131
}
131132
}
132-
ans := 0
133133
for _, c := range nums3 {
134134
for _, d := range nums4 {
135-
ans += counter[-(c + d)]
135+
ans += cnt[-(c + d)]
136136
}
137137
}
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;
139166
}
140167
```
141168

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

+48-25
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,25 @@ class Solution:
5757
def fourSumCount(
5858
self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]
5959
) -> int:
60-
counter = Counter()
61-
for a in nums1:
62-
for b in nums2:
63-
counter[a + b] += 1
64-
ans = 0
65-
for c in nums3:
66-
for d in nums4:
67-
ans += counter[-(c + d)]
68-
return ans
60+
cnt = Counter(a + b for a in nums1 for b in nums2)
61+
return sum(cnt[-(c + d)] for c in nums3 for d in nums4)
6962
```
7063

7164
### **Java**
7265

7366
```java
7467
class Solution {
7568
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
76-
Map<Integer, Integer> counter = new HashMap<>();
69+
Map<Integer, Integer> cnt = new HashMap<>();
7770
for (int a : nums1) {
7871
for (int b : nums2) {
79-
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
72+
cnt.merge(a + b, 1, Integer::sum);
8073
}
8174
}
8275
int ans = 0;
8376
for (int c : nums3) {
8477
for (int d : nums4) {
85-
ans += counter.getOrDefault(-(c + d), 0);
78+
ans += cnt.getOrDefault(-(c + d), 0);
8679
}
8780
}
8881
return ans;
@@ -96,14 +89,18 @@ class Solution {
9689
class Solution {
9790
public:
9891
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
99-
unordered_map<int, int> counter;
100-
for (int a : nums1)
101-
for (int b : nums2)
102-
++counter[a + b];
92+
unordered_map<int, int> cnt;
93+
for (int a : nums1) {
94+
for (int b : nums2) {
95+
++cnt[a + b];
96+
}
97+
}
10398
int ans = 0;
104-
for (int c : nums3)
105-
for (int d : nums4)
106-
ans += counter[-(c + d)];
99+
for (int c : nums3) {
100+
for (int d : nums4) {
101+
ans += cnt[-(c + d)];
102+
}
103+
}
107104
return ans;
108105
}
109106
};
@@ -112,20 +109,46 @@ public:
112109
### **Go**
113110
114111
```go
115-
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
116-
counter := make(map[int]int)
112+
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) {
113+
cnt := map[int]int{}
117114
for _, a := range nums1 {
118115
for _, b := range nums2 {
119-
counter[a+b]++
116+
cnt[a+b]++
120117
}
121118
}
122-
ans := 0
123119
for _, c := range nums3 {
124120
for _, d := range nums4 {
125-
ans += counter[-(c + d)]
121+
ans += cnt[-(c + d)]
126122
}
127123
}
128-
return ans
124+
return
125+
}
126+
```
127+
128+
### **TypeScript**
129+
130+
```ts
131+
function fourSumCount(
132+
nums1: number[],
133+
nums2: number[],
134+
nums3: number[],
135+
nums4: number[],
136+
): number {
137+
const cnt: Map<number, number> = new Map();
138+
for (const a of nums1) {
139+
for (const b of nums2) {
140+
const x = a + b;
141+
cnt.set(x, (cnt.get(x) || 0) + 1);
142+
}
143+
}
144+
let ans = 0;
145+
for (const c of nums3) {
146+
for (const d of nums4) {
147+
const x = c + d;
148+
ans += cnt.get(-x) || 0;
149+
}
150+
}
151+
return ans;
129152
}
130153
```
131154

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
class Solution {
22
public:
33
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];
4+
unordered_map<int, int> cnt;
5+
for (int a : nums1) {
6+
for (int b : nums2) {
7+
++cnt[a + b];
8+
}
9+
}
810
int ans = 0;
9-
for (int c : nums3)
10-
for (int d : nums4)
11-
ans += counter[-(c + d)];
11+
for (int c : nums3) {
12+
for (int d : nums4) {
13+
ans += cnt[-(c + d)];
14+
}
15+
}
1216
return ans;
1317
}
1418
};
+5-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
2-
counter := make(map[int]int)
1+
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) {
2+
cnt := map[int]int{}
33
for _, a := range nums1 {
44
for _, b := range nums2 {
5-
counter[a+b]++
5+
cnt[a+b]++
66
}
77
}
8-
ans := 0
98
for _, c := range nums3 {
109
for _, d := range nums4 {
11-
ans += counter[-(c + d)]
10+
ans += cnt[-(c + d)]
1211
}
1312
}
14-
return ans
13+
return
1514
}

solution/0400-0499/0454.4Sum II/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
3-
Map<Integer, Integer> counter = new HashMap<>();
3+
Map<Integer, Integer> cnt = new HashMap<>();
44
for (int a : nums1) {
55
for (int b : nums2) {
6-
counter.put(a + b, counter.getOrDefault(a + b, 0) + 1);
6+
cnt.merge(a + b, 1, Integer::sum);
77
}
88
}
99
int ans = 0;
1010
for (int c : nums3) {
1111
for (int d : nums4) {
12-
ans += counter.getOrDefault(-(c + d), 0);
12+
ans += cnt.getOrDefault(-(c + d), 0);
1313
}
1414
}
1515
return ans;

solution/0400-0499/0454.4Sum II/Solution.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,5 @@ class Solution:
22
def fourSumCount(
33
self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]
44
) -> int:
5-
counter = Counter()
6-
for a in nums1:
7-
for b in nums2:
8-
counter[a + b] += 1
9-
ans = 0
10-
for c in nums3:
11-
for d in nums4:
12-
ans += counter[-(c + d)]
13-
return ans
5+
cnt = Counter(a + b for a in nums1 for b in nums2)
6+
return sum(cnt[-(c + d)] for c in nums3 for d in nums4)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function fourSumCount(
2+
nums1: number[],
3+
nums2: number[],
4+
nums3: number[],
5+
nums4: number[],
6+
): number {
7+
const cnt: Map<number, number> = new Map();
8+
for (const a of nums1) {
9+
for (const b of nums2) {
10+
const x = a + b;
11+
cnt.set(x, (cnt.get(x) || 0) + 1);
12+
}
13+
}
14+
let ans = 0;
15+
for (const c of nums3) {
16+
for (const d of nums4) {
17+
const x = c + d;
18+
ans += cnt.get(-x) || 0;
19+
}
20+
}
21+
return ans;
22+
}

solution/0600-0699/0677.Map Sum Pairs/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def search(self, w: str) -> int:
2323

2424

2525
class MapSum:
26-
2726
def __init__(self):
2827
self.d = defaultdict(int)
2928
self.tree = Trie()
@@ -36,6 +35,7 @@ def insert(self, key: str, val: int) -> None:
3635
def sum(self, prefix: str) -> int:
3736
return self.tree.search(prefix)
3837

38+
3939
# Your MapSum object will be instantiated and called as such:
4040
# obj = MapSum()
4141
# obj.insert(key,val)

solution/1000-1099/1001.Grid Illumination/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
2+
def gridIllumination(
3+
self, n: int, lamps: List[List[int]], queries: List[List[int]]
4+
) -> List[int]:
35
s = {(i, j) for i, j in lamps}
46
row, col, diag1, diag2 = Counter(), Counter(), Counter(), Counter()
57
for i, j in s:

solution/2400-2499/2429.Minimize XOR/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ def minimizeXor(self, num1: int, num2: int) -> int:
33
cnt1 = num1.bit_count()
44
cnt2 = num2.bit_count()
55
while cnt1 > cnt2:
6-
num1 &= (num1 - 1)
6+
num1 &= num1 - 1
77
cnt1 -= 1
88
while cnt1 < cnt2:
9-
num1 |= (num1 + 1)
9+
num1 |= num1 + 1
1010
cnt1 += 1
1111
return num1

0 commit comments

Comments
 (0)