Skip to content

Commit 2908d74

Browse files
authored
Create solution.cpp
1 parent f0e7ac8 commit 2908d74

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class SparseVector {
2+
public:
3+
vector<int> numbers;
4+
5+
SparseVector(vector<int> &nums) : numbers(nums)
6+
{
7+
8+
}
9+
10+
// Return the dotProduct of two sparse vectors
11+
int dotProduct(SparseVector& vec)
12+
{
13+
int sum = 0;
14+
15+
for (int i=0; i<numbers.size(); i++)
16+
{
17+
// multiply each ith element between both vectors together
18+
// adding the result to the total sum
19+
sum += numbers.at(i) * vec.numbers.at(i);
20+
}
21+
22+
return sum;
23+
}
24+
};
25+
26+
// Your SparseVector object will be instantiated and called as such:
27+
// SparseVector v1(nums1);
28+
// SparseVector v2(nums2);
29+
// int ans = v1.dotProduct(v2);

0 commit comments

Comments
 (0)