Sparse Matrix Multiplication

Problem: Sparse Matrix Multiplication

In this question, we only need to calculate the non-zero numbers.

Code in Python:

class Solution(object):
    def multiply(self, A, B):
        """
        :type A: List[List[int]]
        :type B: List[List[int]]
        :rtype: List[List[int]]
        """
        ra, ca = len(A), len(A[0])
        rb, cb = len(B), len(B[0])
        r, c = ra, cb
        ans = [[0] * c for _ in xrange(r)]
        for i in xrange(ra):
            for j in xrange(ca):
                if A[i][j]:
                    for k in xrange(cb):
                        ans[i][k] += A[i][j] * B[j][k]
        return ans

results matching ""

    No results matching ""