@@ -34,7 +34,6 @@ A</strong> = [
34
34
| 0 0 1 |
35
35
</pre >
36
36
37
-
38
37
## 解法
39
38
40
39
<!-- 这里可写通用的实现逻辑 -->
@@ -45,16 +44,124 @@ A</strong> = [
45
44
46
45
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
46
47
+ 直接模拟。
48
+
48
49
``` python
50
+ class Solution :
51
+ def multiply (self , mat1 : List[List[int ]], mat2 : List[List[int ]]) -> List[List[int ]]:
52
+ r1, c1, c2 = len (mat1), len (mat1[0 ]), len (mat2[0 ])
53
+ res = [[0 ] * c2 for _ in range (r1)]
54
+ for i in range (r1):
55
+ for j in range (c2):
56
+ for k in range (c1):
57
+ res[i][j] += mat1[i][k] * mat2[k][j]
58
+ return res
59
+ ```
60
+
61
+ 用哈希表记录稀疏矩阵 mat1 中的非 0 值。
49
62
63
+ ``` python
64
+ class Solution :
65
+ def multiply (self , mat1 : List[List[int ]], mat2 : List[List[int ]]) -> List[List[int ]]:
66
+ r1, c1, c2 = len (mat1), len (mat1[0 ]), len (mat2[0 ])
67
+ res = [[0 ] * c2 for _ in range (r1)]
68
+ mp = collections.defaultdict(list )
69
+ for i in range (r1):
70
+ for j in range (c1):
71
+ if mat1[i][j] != 0 :
72
+ mp[i].append(j)
73
+ for i in range (r1):
74
+ for j in range (c2):
75
+ for k in mp[i]:
76
+ res[i][j] += mat1[i][k] * mat2[k][j]
77
+ return res
50
78
```
51
79
52
80
### ** Java**
53
81
54
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
83
56
84
``` java
85
+ class Solution {
86
+ public int [][] multiply (int [][] mat1 , int [][] mat2 ) {
87
+ int r1 = mat1. length, c1 = mat1[0 ]. length, c2 = mat2[0 ]. length;
88
+ int [][] res = new int [r1][c2];
89
+ Map<Integer , List<Integer > > mp = new HashMap<> ();
90
+ for (int i = 0 ; i < r1; ++ i) {
91
+ for (int j = 0 ; j < c1; ++ j) {
92
+ if (mat1[i][j] != 0 ) {
93
+ mp. computeIfAbsent(i, k - > new ArrayList<> ()). add(j);
94
+ }
95
+ }
96
+ }
97
+ for (int i = 0 ; i < r1; ++ i) {
98
+ for (int j = 0 ; j < c2; ++ j) {
99
+ if (mp. containsKey(i)) {
100
+ for (int k : mp. get(i)) {
101
+ res[i][j] += mat1[i][k] * mat2[k][j];
102
+ }
103
+ }
104
+ }
105
+ }
106
+ return res;
107
+ }
108
+ }
109
+ ```
110
+
111
+ ### ** C++**
112
+
113
+ ``` cpp
114
+ class Solution {
115
+ public:
116
+ vector<vector<int >> multiply(vector<vector<int >>& mat1, vector<vector<int >>& mat2) {
117
+ int r1 = mat1.size(), c1 = mat1[ 0] .size(), c2 = mat2[ 0] .size();
118
+ vector<vector<int >> res(r1, vector<int >(c2));
119
+ unordered_map<int, vector<int >> mp;
120
+ for (int i = 0; i < r1; ++i)
121
+ {
122
+ for (int j = 0; j < c1; ++j)
123
+ {
124
+ if (mat1[ i] [ j ] != 0) mp[ i] .push_back(j);
125
+ }
126
+ }
127
+ for (int i = 0; i < r1; ++i)
128
+ {
129
+ for (int j = 0; j < c2; ++j)
130
+ {
131
+ for (int k : mp[ i] ) res[ i] [ j ] += mat1[ i] [ k ] * mat2[ k] [ j ] ;
132
+ }
133
+ }
134
+ return res;
135
+ }
136
+ };
137
+ ```
57
138
139
+ ### **Go**
140
+
141
+ ```go
142
+ func multiply(mat1 [][]int, mat2 [][]int) [][]int {
143
+ r1, c1, c2 := len(mat1), len(mat1[0]), len(mat2[0])
144
+ res := make([][]int, r1)
145
+ for i := range res {
146
+ res[i] = make([]int, c2)
147
+ }
148
+ mp := make(map[int][]int)
149
+ for i := 0; i < r1; i++ {
150
+ for j := 0; j < c1; j++ {
151
+ if mat1[i][j] != 0 {
152
+ mp[i] = append(mp[i], j)
153
+ }
154
+ }
155
+ }
156
+ for i := 0; i < r1; i++ {
157
+ for j := 0; j < c2; j++ {
158
+ for _, k := range mp[i] {
159
+ res[i][j] += mat1[i][k] * mat2[k][j]
160
+ }
161
+ }
162
+ }
163
+ return res
164
+ }
58
165
```
59
166
60
167
### ** ...**
0 commit comments