Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7928aed

Browse files
feat: add solutions to lc problem: No.1570 (doocs#4300)
No.1570.Dot Product of Two Sparse Vectors
1 parent 350a49f commit 7928aed

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

‎solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
7777

7878
### 方法一:哈希表
7979

80-
我们用哈希表 $d$ 来存储非零元素,其中键为下标,值为对应的值。我们遍历 $nums,ドル如果 $nums[i]$ 不为 0ドル,ドル我们就将 $(i, nums[i])$ 加入到哈希表 $d$ 中。
80+
我们用哈希表 $d$ 来存储非零元素,其中键为下标,值为对应的值。我们遍历 $\textit{nums},ドル如果 $\textit{nums}[i]$ 不为 0ドル,ドル我们就将 $(i, \textit{nums}[i])$ 加入到哈希表 $d$ 中。
8181

8282
在计算点积时,我们遍历非零元素较少的哈希表,并判断另一个哈希表中是否存在对应的键,如果存在就将对应的值相乘并累加到答案中。
8383

@@ -261,6 +261,46 @@ class SparseVector {
261261
*/
262262
```
263263

264+
#### Rust
265+
266+
```rust
267+
use std::collections::HashMap;
268+
269+
#[derive(Clone)]
270+
struct SparseVector {
271+
d: HashMap<usize, i32>,
272+
}
273+
274+
impl SparseVector {
275+
fn new(nums: Vec<i32>) -> Self {
276+
let mut d = HashMap::new();
277+
for (i, &x) in nums.iter().enumerate() {
278+
if x != 0 {
279+
d.insert(i, x);
280+
}
281+
}
282+
SparseVector { d }
283+
}
284+
285+
fn dot_product(&self, vec: SparseVector) -> i32 {
286+
let (a, b) = (&self.d, &vec.d);
287+
let mut ans = 0;
288+
289+
if a.len() > b.len() {
290+
return vec.dot_product(self.clone());
291+
}
292+
293+
for (&i, &x) in a.iter() {
294+
if let Some(&y) = b.get(&i) {
295+
ans += x * y;
296+
}
297+
}
298+
299+
ans
300+
}
301+
}
302+
```
303+
264304
<!-- tabs:end -->
265305

266306
<!-- solution:end -->

‎solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README_EN.md‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Hash Map
77+
78+
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$.
79+
80+
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.
81+
82+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the length of the array.
7783

7884
<!-- tabs:start -->
7985

@@ -253,6 +259,46 @@ class SparseVector {
253259
*/
254260
```
255261

262+
#### Rust
263+
264+
```rust
265+
use std::collections::HashMap;
266+
267+
#[derive(Clone)]
268+
struct SparseVector {
269+
d: HashMap<usize, i32>,
270+
}
271+
272+
impl SparseVector {
273+
fn new(nums: Vec<i32>) -> Self {
274+
let mut d = HashMap::new();
275+
for (i, &x) in nums.iter().enumerate() {
276+
if x != 0 {
277+
d.insert(i, x);
278+
}
279+
}
280+
SparseVector { d }
281+
}
282+
283+
fn dot_product(&self, vec: SparseVector) -> i32 {
284+
let (a, b) = (&self.d, &vec.d);
285+
let mut ans = 0;
286+
287+
if a.len() > b.len() {
288+
return vec.dot_product(self.clone());
289+
}
290+
291+
for (&i, &x) in a.iter() {
292+
if let Some(&y) = b.get(&i) {
293+
ans += x * y;
294+
}
295+
}
296+
297+
ans
298+
}
299+
}
300+
```
301+
256302
<!-- tabs:end -->
257303

258304
<!-- solution:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::collections::HashMap;
2+
3+
#[derive(Clone)]
4+
struct SparseVector {
5+
d: HashMap<usize, i32>,
6+
}
7+
8+
impl SparseVector {
9+
fn new(nums: Vec<i32>) -> Self {
10+
let mut d = HashMap::new();
11+
for (i, &x) in nums.iter().enumerate() {
12+
if x != 0 {
13+
d.insert(i, x);
14+
}
15+
}
16+
SparseVector { d }
17+
}
18+
19+
fn dot_product(&self, vec: SparseVector) -> i32 {
20+
let (a, b) = (&self.d, &vec.d);
21+
let mut ans = 0;
22+
23+
if a.len() > b.len() {
24+
return vec.dot_product(self.clone());
25+
}
26+
27+
for (&i, &x) in a.iter() {
28+
if let Some(&y) = b.get(&i) {
29+
ans += x * y;
30+
}
31+
}
32+
33+
ans
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /