diff --git a/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md index 2a47af11bbc73..ba340420a0b91 100644 --- a/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md +++ b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md @@ -77,7 +77,7 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0 ### 方法一:哈希表 -我们用哈希表 $d$ 来存储非零元素,其中键为下标,值为对应的值。我们遍历 $nums$,如果 $nums[i]$ 不为 $0$,我们就将 $(i, nums[i])$ 加入到哈希表 $d$ 中。 +我们用哈希表 $d$ 来存储非零元素,其中键为下标,值为对应的值。我们遍历 $\textit{nums}$,如果 $\textit{nums}[i]$ 不为 $0$,我们就将 $(i, \textit{nums}[i])$ 加入到哈希表 $d$ 中。 在计算点积时,我们遍历非零元素较少的哈希表,并判断另一个哈希表中是否存在对应的键,如果存在就将对应的值相乘并累加到答案中。 @@ -261,6 +261,46 @@ class SparseVector { */ ``` +#### Rust + +```rust +use std::collections::HashMap; + +#[derive(Clone)] +struct SparseVector { + d: HashMap, +} + +impl SparseVector { + fn new(nums: Vec) -> Self { + let mut d = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + if x != 0 { + d.insert(i, x); + } + } + SparseVector { d } + } + + fn dot_product(&self, vec: SparseVector) -> i32 { + let (a, b) = (&self.d, &vec.d); + let mut ans = 0; + + if a.len() > b.len() { + return vec.dot_product(self.clone()); + } + + for (&i, &x) in a.iter() { + if let Some(&y) = b.get(&i) { + ans += x * y; + } + } + + ans + } +} +``` + diff --git a/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README_EN.md b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README_EN.md index 5aa2c99489ba6..b57560270588e 100644 --- a/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README_EN.md +++ b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README_EN.md @@ -73,7 +73,13 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0 -### Solution 1 +### Solution 1: Hash Map + +We use a hash map $d$ to store non-zero elements, where the key is the index, and the value is the corresponding value. We iterate through $\textit{nums}$, and if $\textit{nums}[i]$ is not $0$, we add $(i, \textit{nums}[i])$ to the hash map $d$. + +When calculating the dot product, we iterate through the hash map with fewer non-zero elements and check if the other hash map contains the corresponding key. If it exists, we multiply the corresponding values and add them to the result. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array. @@ -253,6 +259,46 @@ class SparseVector { */ ``` +#### Rust + +```rust +use std::collections::HashMap; + +#[derive(Clone)] +struct SparseVector { + d: HashMap, +} + +impl SparseVector { + fn new(nums: Vec) -> Self { + let mut d = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + if x != 0 { + d.insert(i, x); + } + } + SparseVector { d } + } + + fn dot_product(&self, vec: SparseVector) -> i32 { + let (a, b) = (&self.d, &vec.d); + let mut ans = 0; + + if a.len() > b.len() { + return vec.dot_product(self.clone()); + } + + for (&i, &x) in a.iter() { + if let Some(&y) = b.get(&i) { + ans += x * y; + } + } + + ans + } +} +``` + diff --git a/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/Solution.rs b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/Solution.rs new file mode 100644 index 0000000000000..31888739a54e6 --- /dev/null +++ b/solution/1500-1599/1570.Dot Product of Two Sparse Vectors/Solution.rs @@ -0,0 +1,35 @@ +use std::collections::HashMap; + +#[derive(Clone)] +struct SparseVector { + d: HashMap, +} + +impl SparseVector { + fn new(nums: Vec) -> Self { + let mut d = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + if x != 0 { + d.insert(i, x); + } + } + SparseVector { d } + } + + fn dot_product(&self, vec: SparseVector) -> i32 { + let (a, b) = (&self.d, &vec.d); + let mut ans = 0; + + if a.len() > b.len() { + return vec.dot_product(self.clone()); + } + + for (&i, &x) in a.iter() { + if let Some(&y) = b.get(&i) { + ans += x * y; + } + } + + ans + } +}