-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path311. Sparse Matrix Multiplication.c
65 lines (52 loc) · 1.37 KB
/
311. Sparse Matrix Multiplication.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
311. Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB.
You may assume that A's column number is equal to B's row number.
Example:
A = [
[ 1, 0, 0],
[-1, 0, 3]
]
B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
| 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
*/
/**
* Return an array of arrays.
* Note: The returned array must be malloced, assume caller calls free().
*/
int** multiply(int** A, int ARowSize, int AColSize, int** B, int BRowSize, int BColSize) {
int **result, *buff;
int i, j, k;
result = malloc(ARowSize * sizeof(int *));
//assert(result);
for (i = 0; i < ARowSize; i ++) {
buff = calloc(BColSize, sizeof(int));
//assert(buff);
result[i] = buff;
for (j = 0; j < AColSize; j ++) {
if (A[i][j] != 0) {
//printf("\nAij: %d\n", A[i][j]);
for (k = 0; k < BColSize; k ++) {
//printf("Bjk: %d, ", B[j][k]);
buff[k] += A[i][j] * B[j][k];
}
}
}
}
return result;
}
/*
Difficulty:Medium
Total Accepted:30.5K
Total Submissions:60.1K
Companies LinkedIn Facebook
Related Topics Hash Table
*/