-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot-product.cpp
33 lines (27 loc) · 920 Bytes
/
dot-product.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
Given two sparse vectors, compute their dot product.
Implement class SparseVector:
SparseVector(nums) Initializes the object with the vector nums
dotProduct(vec) Compute the dot product between the instance of SparseVector and vec
A sparse vector is a vector that has mostly zero values,
you should store the sparse vector efficiently and compute the dot product between two SparseVector.
*/
class SparseVector {
public:
vector<int> temp;
SparseVector(vector<int> &nums) {
this->temp = nums;
}
// Return the dotProduct of two sparse vectors
int dotProduct(SparseVector& vec) {
int ans = 0;
for(int i=0;i<vec.temp.size();i++){
ans += (temp[i]*vec.temp[i]);
}
return ans;
}
};
// Your SparseVector object will be instantiated and called as such:
// SparseVector v1(nums1);
// SparseVector v2(nums2);
// int ans = v1.dotProduct(v2);