Ruby solution beats 100%


  • 0
    T
    def multiply(a, b)
      ar = a.length
      ac = a[0].length
      bc = b[0].length
      result = Array.new(ar, 0){Array.new(bc, 0)}
    
      ar.times do |i|
        (0...ac).each do |n|
          next if a[i][n] == 0
          bc.times do |j|
            next if b[n][j] == 0
            result[i][j] += a[i][n] * b[n][j]
          end
        end
      end
      result
    end
    

Log in to reply
 

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