# Leetcode: Sparse Matrix Multiplication

Sparse Matrix Multiplication Similar Problems:

Given two sparse matrices A and B, return the result of AB.

You may assume that A’s column number is equal to B’s row number.

Example:

```A = [
[ 1, 0, 0],
[-1, 0, 3]
]

B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]

|  1 0 0 |   | 7 0 0 |   |  7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
```

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

```## Blog link: https://code.dennyzhang.com/sparse-matrix-multiplication
## Basic Ideas:
##
## Complexity:
class Solution:
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
if len(A) == 0 or len(B) == 0: return []
# reverse B
B2 = [[None for j in range(len(B))] for i in range(len(B))]
for i in range(len(B)):
for j in range(len(B)):
B2[j][i] = B[i][j]

C = [[0 for j in range(len(B))] for i in range(len(A))]
for i in range(len(A)):
if A[i] == *len(A): continue
for j in range(len(B)):
if B2[j] == *len(B2): continue
for k in range(len(A)):
C[i][j] += A[i][k]*B[k][j]
return C
```

Share It, If You Like It.