Python Readable solution

  • 0

    Here is the readable sol. A lot of time I don't understand ppl's code (maybe just me), so I prefer to have readable code, than seed and fancy code. Any feedback is great!

    def multiply(self, A, B):
            AB = [[0] * len(B[0]) for _ in range(len(A))]
            BIsZeroCol = [sum(col) for col in zip(*B)]
            AIsZeroRow = [sum(row) for row in A]
            def getAB(x, y, AB):
                if not AIsZeroRow[y] or not BIsZeroCol[x]:
                for i in range(len(A[0])):
                    if A[y][i] != 0 and B[i][x] != 0:
                        AB[y][x] += A[y][i] * B[i][x] 
            [getAB(x, y, AB) for x in range(len(B[0])) for y in range(len(A))]
            return AB

Log in to reply

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