Skip to content

feat: add solutions to lc problem: No.1570 #4300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 中。

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

Expand Down Expand Up @@ -261,6 +261,46 @@ class SparseVector {
*/
```

#### Rust

```rust
use std::collections::HashMap;

#[derive(Clone)]
struct SparseVector {
d: HashMap<usize, i32>,
}

impl SparseVector {
fn new(nums: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -253,6 +259,46 @@ class SparseVector {
*/
```

#### Rust

```rust
use std::collections::HashMap;

#[derive(Clone)]
struct SparseVector {
d: HashMap<usize, i32>,
}

impl SparseVector {
fn new(nums: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

#[derive(Clone)]
struct SparseVector {
d: HashMap<usize, i32>,
}

impl SparseVector {
fn new(nums: Vec<i32>) -> 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
}
}