Skip to content

Commit 70dda00

Browse files
refactor 311
1 parent db42028 commit 70dda00

File tree

1 file changed

+21
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-16
lines changed
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

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.
47
58
You may assume that A's column number is equal to B's row number.
69
@@ -17,29 +20,31 @@
1720
[ 0, 0, 1 ]
1821
]
1922
20-
2123
| 1 0 0 | | 7 0 0 | | 7 0 0 |
2224
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
23-
| 0 0 1 |*/
25+
| 0 0 1 |
26+
27+
*/
2428
public class _311 {
2529

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+
}
3743
}
3844
}
3945
}
4046
}
47+
return C;
4148
}
42-
return C;
4349
}
44-
4550
}

0 commit comments

Comments
 (0)