We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f0e7ac8 commit 2908d74Copy full SHA for 2908d74
DotProductOfTwoSparseVectors/solution.cpp
@@ -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