-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiply the matrices
More file actions
31 lines (28 loc) · 834 Bytes
/
Multiply the matrices
File metadata and controls
31 lines (28 loc) · 834 Bytes
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
class Solution
{
public:
vector<vector<int> > multiplyMatrix( const vector<vector<int> >& A, const vector<vector<int> >& B)
{
int n1 = A.size();
int m1 = A[0].size();
int n2 = B.size();
int m2 = B[0].size();
vector<vector<int> > result;
if(m1 == n2){
result.resize(n1);
for(int i=0; i<n1; i++){
result[i].assign(m2,0);
for(int j=0; j<m2; j++){
//sum to store intermediate of
//row and column multiplication
int sum=0;
for(int k=0; k<m1; k++){
sum += A[i][k] * B[k][j];
}
result[i][j] = sum;
}
}
}
return result;
}
};