Simple python solution beats 100%


  • 0
    Z

    Use a vector to represent matrix A

    class Solution(object):
    def multiply(self, A, B):
        """
        :type A: List[List[int]]
        :type B: List[List[int]]
        :rtype: List[List[int]]
        """
        m, p = len(A), len(B[0])
        Ai = [[i for i, v in enumerate(r) if v] if any(r) else [] for r in A]
        res = [[0] * p for i in range(m)]
        for i, r in enumerate(Ai):
            if not r:
                continue
            for k in range(p):
                for j, v in enumerate(r):
                    res[i][k] += A[i][v] * B[v][k]
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.