Skip to content

Commit d6db065

Browse files
committed
feat: add solutions to lc problem: No.0311.Sparse Matrix Multiplication
1 parent 1ad685a commit d6db065

File tree

8 files changed

+296
-23
lines changed

8 files changed

+296
-23
lines changed

solution/0300-0399/0311.Sparse Matrix Multiplication/README.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ A</strong> = [
3434
| 0 0 1 |
3535
</pre>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
@@ -45,16 +44,124 @@ A</strong> = [
4544

4645
<!-- 这里可写当前语言的特殊实现逻辑 -->
4746

47+
直接模拟。
48+
4849
```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 值。
4962

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
5078
```
5179

5280
### **Java**
5381

5482
<!-- 这里可写当前语言的特殊实现逻辑 -->
5583

5684
```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+
```
57138
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+
}
58165
```
59166

60167
### **...**

solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,124 @@
3232
<li><code>-100 &lt;= mat1[i][j], mat2[i][j] &lt;= 100</code></li>
3333
</ul>
3434

35-
3635
## Solutions
3736

3837
<!-- tabs:start -->
3938

4039
### **Python3**
4140

4241
```python
42+
class Solution:
43+
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
44+
r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0])
45+
res = [[0] * c2 for _ in range(r1)]
46+
for i in range(r1):
47+
for j in range(c2):
48+
for k in range(c1):
49+
res[i][j] += mat1[i][k] * mat2[k][j]
50+
return res
51+
```
4352

53+
```python
54+
class Solution:
55+
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
56+
r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0])
57+
res = [[0] * c2 for _ in range(r1)]
58+
mp = collections.defaultdict(list)
59+
for i in range(r1):
60+
for j in range(c1):
61+
if mat1[i][j] != 0:
62+
mp[i].append(j)
63+
for i in range(r1):
64+
for j in range(c2):
65+
for k in mp[i]:
66+
res[i][j] += mat1[i][k] * mat2[k][j]
67+
return res
4468
```
4569

4670
### **Java**
4771

4872
```java
73+
class Solution {
74+
public int[][] multiply(int[][] mat1, int[][] mat2) {
75+
int r1 = mat1.length, c1 = mat1[0].length, c2 = mat2[0].length;
76+
int[][] res = new int[r1][c2];
77+
Map<Integer, List<Integer>> mp = new HashMap<>();
78+
for (int i = 0; i < r1; ++i) {
79+
for (int j = 0; j < c1; ++j) {
80+
if (mat1[i][j] != 0) {
81+
mp.computeIfAbsent(i, k -> new ArrayList<>()).add(j);
82+
}
83+
}
84+
}
85+
for (int i = 0; i < r1; ++i) {
86+
for (int j = 0; j < c2; ++j) {
87+
if (mp.containsKey(i)) {
88+
for (int k : mp.get(i)) {
89+
res[i][j] += mat1[i][k] * mat2[k][j];
90+
}
91+
}
92+
}
93+
}
94+
return res;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
vector<vector<int>> multiply(vector<vector<int>>& mat1, vector<vector<int>>& mat2) {
105+
int r1 = mat1.size(), c1 = mat1[0].size(), c2 = mat2[0].size();
106+
vector<vector<int>> res(r1, vector<int>(c2));
107+
unordered_map<int, vector<int>> mp;
108+
for (int i = 0; i < r1; ++i)
109+
{
110+
for (int j = 0; j < c1; ++j)
111+
{
112+
if (mat1[i][j] != 0) mp[i].push_back(j);
113+
}
114+
}
115+
for (int i = 0; i < r1; ++i)
116+
{
117+
for (int j = 0; j < c2; ++j)
118+
{
119+
for (int k : mp[i]) res[i][j] += mat1[i][k] * mat2[k][j];
120+
}
121+
}
122+
return res;
123+
}
124+
};
125+
```
49126
127+
### **Go**
128+
129+
```go
130+
func multiply(mat1 [][]int, mat2 [][]int) [][]int {
131+
r1, c1, c2 := len(mat1), len(mat1[0]), len(mat2[0])
132+
res := make([][]int, r1)
133+
for i := range res {
134+
res[i] = make([]int, c2)
135+
}
136+
mp := make(map[int][]int)
137+
for i := 0; i < r1; i++ {
138+
for j := 0; j < c1; j++ {
139+
if mat1[i][j] != 0 {
140+
mp[i] = append(mp[i], j)
141+
}
142+
}
143+
}
144+
for i := 0; i < r1; i++ {
145+
for j := 0; j < c2; j++ {
146+
for _, k := range mp[i] {
147+
res[i][j] += mat1[i][k] * mat2[k][j]
148+
}
149+
}
150+
}
151+
return res
152+
}
50153
```
51154

52155
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> multiply(vector<vector<int>>& mat1, vector<vector<int>>& mat2) {
4+
int r1 = mat1.size(), c1 = mat1[0].size(), c2 = mat2[0].size();
5+
vector<vector<int>> res(r1, vector<int>(c2));
6+
unordered_map<int, vector<int>> mp;
7+
for (int i = 0; i < r1; ++i)
8+
{
9+
for (int j = 0; j < c1; ++j)
10+
{
11+
if (mat1[i][j] != 0) mp[i].push_back(j);
12+
}
13+
}
14+
for (int i = 0; i < r1; ++i)
15+
{
16+
for (int j = 0; j < c2; ++j)
17+
{
18+
for (int k : mp[i]) res[i][j] += mat1[i][k] * mat2[k][j];
19+
}
20+
}
21+
return res;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func multiply(mat1 [][]int, mat2 [][]int) [][]int {
2+
r1, c1, c2 := len(mat1), len(mat1[0]), len(mat2[0])
3+
res := make([][]int, r1)
4+
for i := range res {
5+
res[i] = make([]int, c2)
6+
}
7+
mp := make(map[int][]int)
8+
for i := 0; i < r1; i++ {
9+
for j := 0; j < c1; j++ {
10+
if mat1[i][j] != 0 {
11+
mp[i] = append(mp[i], j)
12+
}
13+
}
14+
}
15+
for i := 0; i < r1; i++ {
16+
for j := 0; j < c2; j++ {
17+
for _, k := range mp[i] {
18+
res[i][j] += mat1[i][k] * mat2[k][j]
19+
}
20+
}
21+
}
22+
return res
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[][] multiply(int[][] mat1, int[][] mat2) {
3+
int r1 = mat1.length, c1 = mat1[0].length, c2 = mat2[0].length;
4+
int[][] res = new int[r1][c2];
5+
Map<Integer, List<Integer>> mp = new HashMap<>();
6+
for (int i = 0; i < r1; ++i) {
7+
for (int j = 0; j < c1; ++j) {
8+
if (mat1[i][j] != 0) {
9+
mp.computeIfAbsent(i, k -> new ArrayList<>()).add(j);
10+
}
11+
}
12+
}
13+
for (int i = 0; i < r1; ++i) {
14+
for (int j = 0; j < c2; ++j) {
15+
if (mp.containsKey(i)) {
16+
for (int k : mp.get(i)) {
17+
res[i][j] += mat1[i][k] * mat2[k][j];
18+
}
19+
}
20+
}
21+
}
22+
return res;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
3+
r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0])
4+
res = [[0] * c2 for _ in range(r1)]
5+
mp = collections.defaultdict(list)
6+
for i in range(r1):
7+
for j in range(c1):
8+
if mat1[i][j] != 0:
9+
mp[i].append(j)
10+
for i in range(r1):
11+
for j in range(c2):
12+
for k in mp[i]:
13+
res[i][j] += mat1[i][k] * mat2[k][j]
14+
return res

solution/1500-1599/1512.Number of Good Pairs/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
4343
</ul>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->

0 commit comments

Comments
 (0)