Skip to content

Commit ecef1ba

Browse files
authoredJun 5, 2024
feat: add solutions to lc problem: No.454 (#3034)
No.0454.4Sum II
1 parent f5d8b8a commit ecef1ba

File tree

4 files changed

+103
-11
lines changed

4 files changed

+103
-11
lines changed
 

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

+34-3
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int)
152152

153153
```ts
154154
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
155-
const cnt: Map<number, number> = new Map();
155+
const cnt: Record<number, number> = {};
156156
for (const a of nums1) {
157157
for (const b of nums2) {
158158
const x = a + b;
159-
cnt.set(x, (cnt.get(x) || 0) + 1);
159+
cnt[x] = (cnt[x] || 0) + 1;
160160
}
161161
}
162162
let ans = 0;
163163
for (const c of nums3) {
164164
for (const d of nums4) {
165165
const x = c + d;
166-
ans += cnt.get(-x) || 0;
166+
ans += cnt[-x] || 0;
167167
}
168168
}
169169
return ans;
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
use std::collections::HashMap;
177+
178+
impl Solution {
179+
pub fn four_sum_count(
180+
nums1: Vec<i32>,
181+
nums2: Vec<i32>,
182+
nums3: Vec<i32>,
183+
nums4: Vec<i32>
184+
) -> i32 {
185+
let mut cnt = HashMap::new();
186+
for &a in &nums1 {
187+
for &b in &nums2 {
188+
*cnt.entry(a + b).or_insert(0) += 1;
189+
}
190+
}
191+
let mut ans = 0;
192+
for &c in &nums3 {
193+
for &d in &nums4 {
194+
if let Some(&count) = cnt.get(&(0 - (c + d))) {
195+
ans += count;
196+
}
197+
}
198+
}
199+
ans
200+
}
201+
}
202+
```
203+
173204
<!-- tabs:end -->
174205

175206
<!-- solution:end -->

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

+40-5
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ The two tuples are:
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1: HashMap
64+
### Solution 1: Hash Table
6565

66-
Time complexity $O(n^2)$, Space complexity $O(n^2)$.
66+
We can add the elements $a$ and $b$ in arrays $nums1$ and $nums2$ respectively, and store all possible sums in a hash table $cnt$, where the key is the sum of the two numbers, and the value is the count of the sum.
67+
68+
Then we iterate through the elements $c$ and $d$ in arrays $nums3$ and $nums4$, let $c+d$ be the target value, then the answer is the cumulative sum of $cnt[-(c+d)]$.
69+
70+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the length of the array.
6771

6872
<!-- tabs:start -->
6973

@@ -146,24 +150,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int)
146150

147151
```ts
148152
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
149-
const cnt: Map<number, number> = new Map();
153+
const cnt: Record<number, number> = {};
150154
for (const a of nums1) {
151155
for (const b of nums2) {
152156
const x = a + b;
153-
cnt.set(x, (cnt.get(x) || 0) + 1);
157+
cnt[x] = (cnt[x] || 0) + 1;
154158
}
155159
}
156160
let ans = 0;
157161
for (const c of nums3) {
158162
for (const d of nums4) {
159163
const x = c + d;
160-
ans += cnt.get(-x) || 0;
164+
ans += cnt[-x] || 0;
161165
}
162166
}
163167
return ans;
164168
}
165169
```
166170

171+
#### Rust
172+
173+
```rust
174+
use std::collections::HashMap;
175+
176+
impl Solution {
177+
pub fn four_sum_count(
178+
nums1: Vec<i32>,
179+
nums2: Vec<i32>,
180+
nums3: Vec<i32>,
181+
nums4: Vec<i32>
182+
) -> i32 {
183+
let mut cnt = HashMap::new();
184+
for &a in &nums1 {
185+
for &b in &nums2 {
186+
*cnt.entry(a + b).or_insert(0) += 1;
187+
}
188+
}
189+
let mut ans = 0;
190+
for &c in &nums3 {
191+
for &d in &nums4 {
192+
if let Some(&count) = cnt.get(&(0 - (c + d))) {
193+
ans += count;
194+
}
195+
}
196+
}
197+
ans
198+
}
199+
}
200+
```
201+
167202
<!-- tabs:end -->
168203

169204
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn four_sum_count(
5+
nums1: Vec<i32>,
6+
nums2: Vec<i32>,
7+
nums3: Vec<i32>,
8+
nums4: Vec<i32>
9+
) -> i32 {
10+
let mut cnt = HashMap::new();
11+
for &a in &nums1 {
12+
for &b in &nums2 {
13+
*cnt.entry(a + b).or_insert(0) += 1;
14+
}
15+
}
16+
let mut ans = 0;
17+
for &c in &nums3 {
18+
for &d in &nums4 {
19+
if let Some(&count) = cnt.get(&(0 - (c + d))) {
20+
ans += count;
21+
}
22+
}
23+
}
24+
ans
25+
}
26+
}

‎solution/0400-0499/0454.4Sum II/Solution.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
2-
const cnt: Map<number, number> = new Map();
2+
const cnt: Record<number, number> = {};
33
for (const a of nums1) {
44
for (const b of nums2) {
55
const x = a + b;
6-
cnt.set(x, (cnt.get(x) || 0) + 1);
6+
cnt[x] = (cnt[x] || 0) + 1;
77
}
88
}
99
let ans = 0;
1010
for (const c of nums3) {
1111
for (const d of nums4) {
1212
const x = c + d;
13-
ans += cnt.get(-x) || 0;
13+
ans += cnt[-x] || 0;
1414
}
1515
}
1616
return ans;

0 commit comments

Comments
 (0)