File tree Expand file tree Collapse file tree 1 file changed +21
-16
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +21
-16
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
- /**Given two sparse matrices A and B, return the result of AB.
3
+ /**
4
+ * 311. Sparse Matrix Multiplication
5
+ *
6
+ * Given two sparse matrices A and B, return the result of AB.
4
7
5
8
You may assume that A's column number is equal to B's row number.
6
9
17
20
[ 0, 0, 1 ]
18
21
]
19
22
20
-
21
23
| 1 0 0 | | 7 0 0 | | 7 0 0 |
22
24
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
23
- | 0 0 1 |*/
25
+ | 0 0 1 |
26
+
27
+ */
24
28
public class _311 {
25
29
26
- public int [][] multiply (int [][] A , int [][] B ) {
27
- int m = A .length ;
28
- int n = A [0 ].length ;
29
- int p = B [0 ].length ;
30
- int [][] C = new int [m ][p ];
31
- for (int i = 0 ; i < m ; i ++) {
32
- for (int j = 0 ; j < n ; j ++) {
33
- if (A [i ][j ] != 0 ) {
34
- for (int k = 0 ; k < p ; k ++) {
35
- if (B [j ][k ] != 0 ) {
36
- C [i ][k ] += A [i ][j ] * B [j ][k ];
30
+ public static class Solution1 {
31
+ public int [][] multiply (int [][] A , int [][] B ) {
32
+ int m = A .length ;
33
+ int n = A [0 ].length ;
34
+ int p = B [0 ].length ;
35
+ int [][] C = new int [m ][p ];
36
+ for (int i = 0 ; i < m ; i ++) {
37
+ for (int j = 0 ; j < n ; j ++) {
38
+ if (A [i ][j ] != 0 ) {
39
+ for (int k = 0 ; k < p ; k ++) {
40
+ if (B [j ][k ] != 0 ) {
41
+ C [i ][k ] += A [i ][j ] * B [j ][k ];
42
+ }
37
43
}
38
44
}
39
45
}
40
46
}
47
+ return C ;
41
48
}
42
- return C ;
43
49
}
44
-
45
50
}
You can’t perform that action at this time.
0 commit comments