diff --git a/solution/0400-0499/0454.4Sum II/README.md b/solution/0400-0499/0454.4Sum II/README.md index 339135516fcd6..6a3c1e6443f70 100644 --- a/solution/0400-0499/0454.4Sum II/README.md +++ b/solution/0400-0499/0454.4Sum II/README.md @@ -152,24 +152,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) ```ts function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { - const cnt: Map = new Map(); + const cnt: Record = {}; for (const a of nums1) { for (const b of nums2) { const x = a + b; - cnt.set(x, (cnt.get(x) || 0) + 1); + cnt[x] = (cnt[x] || 0) + 1; } } let ans = 0; for (const c of nums3) { for (const d of nums4) { const x = c + d; - ans += cnt.get(-x) || 0; + ans += cnt[-x] || 0; } } return ans; } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn four_sum_count( + nums1: Vec, + nums2: Vec, + nums3: Vec, + nums4: Vec + ) -> i32 { + let mut cnt = HashMap::new(); + for &a in &nums1 { + for &b in &nums2 { + *cnt.entry(a + b).or_insert(0) += 1; + } + } + let mut ans = 0; + for &c in &nums3 { + for &d in &nums4 { + if let Some(&count) = cnt.get(&(0 - (c + d))) { + ans += count; + } + } + } + ans + } +} +``` + diff --git a/solution/0400-0499/0454.4Sum II/README_EN.md b/solution/0400-0499/0454.4Sum II/README_EN.md index ad41470fc8000..484be9eb0356e 100644 --- a/solution/0400-0499/0454.4Sum II/README_EN.md +++ b/solution/0400-0499/0454.4Sum II/README_EN.md @@ -61,9 +61,13 @@ The two tuples are: -### Solution 1: HashMap +### Solution 1: Hash Table -Time complexity $O(n^2)$, Space complexity $O(n^2)$. +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. + +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)]$. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the length of the array. @@ -146,24 +150,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) ```ts function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { - const cnt: Map = new Map(); + const cnt: Record = {}; for (const a of nums1) { for (const b of nums2) { const x = a + b; - cnt.set(x, (cnt.get(x) || 0) + 1); + cnt[x] = (cnt[x] || 0) + 1; } } let ans = 0; for (const c of nums3) { for (const d of nums4) { const x = c + d; - ans += cnt.get(-x) || 0; + ans += cnt[-x] || 0; } } return ans; } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn four_sum_count( + nums1: Vec, + nums2: Vec, + nums3: Vec, + nums4: Vec + ) -> i32 { + let mut cnt = HashMap::new(); + for &a in &nums1 { + for &b in &nums2 { + *cnt.entry(a + b).or_insert(0) += 1; + } + } + let mut ans = 0; + for &c in &nums3 { + for &d in &nums4 { + if let Some(&count) = cnt.get(&(0 - (c + d))) { + ans += count; + } + } + } + ans + } +} +``` + diff --git a/solution/0400-0499/0454.4Sum II/Solution.rs b/solution/0400-0499/0454.4Sum II/Solution.rs new file mode 100644 index 0000000000000..d5cb1dd0b015c --- /dev/null +++ b/solution/0400-0499/0454.4Sum II/Solution.rs @@ -0,0 +1,26 @@ +use std::collections::HashMap; + +impl Solution { + pub fn four_sum_count( + nums1: Vec, + nums2: Vec, + nums3: Vec, + nums4: Vec + ) -> i32 { + let mut cnt = HashMap::new(); + for &a in &nums1 { + for &b in &nums2 { + *cnt.entry(a + b).or_insert(0) += 1; + } + } + let mut ans = 0; + for &c in &nums3 { + for &d in &nums4 { + if let Some(&count) = cnt.get(&(0 - (c + d))) { + ans += count; + } + } + } + ans + } +} diff --git a/solution/0400-0499/0454.4Sum II/Solution.ts b/solution/0400-0499/0454.4Sum II/Solution.ts index 381387f5eecf3..5d88fd29be56c 100644 --- a/solution/0400-0499/0454.4Sum II/Solution.ts +++ b/solution/0400-0499/0454.4Sum II/Solution.ts @@ -1,16 +1,16 @@ function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { - const cnt: Map = new Map(); + const cnt: Record = {}; for (const a of nums1) { for (const b of nums2) { const x = a + b; - cnt.set(x, (cnt.get(x) || 0) + 1); + cnt[x] = (cnt[x] || 0) + 1; } } let ans = 0; for (const c of nums3) { for (const d of nums4) { const x = c + d; - ans += cnt.get(-x) || 0; + ans += cnt[-x] || 0; } } return ans;