-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.java
More file actions
25 lines (23 loc) · 725 Bytes
/
Copy pathMatrix.java
File metadata and controls
25 lines (23 loc) · 725 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
package leetcode;
import java.util.Arrays;
public class Matrix {
public int[][] multiply(int[][] A, int[][] B) {
int rowA = A.length;
int rowB = B.length;
if(rowA == 0 || rowB == 0) return new int[0][0];
int colA = A[0].length;
int colB = B[0].length;
int[][] ret = new int[rowA][colB];
for(int i = 0; i < rowA; i++)
for(int j=0; j<colB; j++){
for(int k=0; k < colA;k++)
ret[i][j] += A[i][k] * B[k][j];
}
return ret;
}
public static void main(String[] args) {
int[][] A = {{1,2},{3,4}};
Matrix matrix = new Matrix();
System.out.println(Arrays.deepToString(matrix.multiply(A, A)));
}
}