forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.cpp
29 lines (28 loc) · 880 Bytes
/
Solution2.cpp
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
27
28
29
class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& mat1, vector<vector<int>>& mat2) {
int m = mat1.size(), n = mat2[0].size();
vector<vector<int>> ans(m, vector<int>(n));
auto g1 = f(mat1), g2 = f(mat2);
for (int i = 0; i < m; ++i) {
for (auto& [k, x] : g1[i]) {
for (auto& [j, y] : g2[k]) {
ans[i][j] += x * y;
}
}
}
return ans;
}
vector<vector<pair<int, int>>> f(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<vector<pair<int, int>>> g(m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j]) {
g[i].emplace_back(j, mat[i][j]);
}
}
}
return g;
}
};