forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.ts
26 lines (26 loc) · 872 Bytes
/
Solution2.ts
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
function multiply(mat1: number[][], mat2: number[][]): number[][] {
const [m, n] = [mat1.length, mat2[0].length];
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
const f = (mat: number[][]): number[][][] => {
const [m, n] = [mat.length, mat[0].length];
const ans: number[][][] = Array.from({ length: m }, () => []);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] !== 0) {
ans[i].push([j, mat[i][j]]);
}
}
}
return ans;
};
const g1 = f(mat1);
const g2 = f(mat2);
for (let i = 0; i < m; ++i) {
for (const [k, x] of g1[i]) {
for (const [j, y] of g2[k]) {
ans[i][j] += x * y;
}
}
}
return ans;
}