simulation/sparse-matrix-multiplication
优质
小牛编辑
131浏览
2023-12-01
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 |
Constraints:
1 <= A.length, B.length <= 100
1 <= A[i].length, B[i].length <= 100
-100 <= A[i][j], B[i][j] <= 100
分析
无
代码
// Sparse Matrix Multiplication
// Time Complexity: O(m*n*p), Space Complexity: O(1)
public class Solution {
public int[][] multiply(int[][] A, int[][] B) {
int m = A.length, n = A[0].length, p = B[0].length;
int[][] C = new int[m][p];
for(int i = 0; i < m; i++) {
for(int k = 0; k < n; k++) {
if (A[i][k] != 0) {
for (int j = 0; j < p; j++) {
if (B[k][j] != 0) C[i][j] += A[i][k] * B[k][j];
}
}
}
}
return C;
}
}
// TODO