facebook 311 Sparse Matrix Multiplication
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了facebook 311 Sparse Matrix Multiplication相关的知识,希望对你有一定的参考价值。
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 |
public class Solution { public int[][] multiply(int[][] A, int[][] B) { int m = A.length; int n = A[0].length; int bn = B[0].length; int[][] C = new int[m][bn]; for (int i=0; i<m; i++) { for (int k=0; k<n; k++) { if (A[i][k] == 0) continue; for (int j=0; j<bn; j++) { C[i][j] += A[i][k] * B[k][j]; } } } return C; } }
以上是关于facebook 311 Sparse Matrix Multiplication的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 311: Sparse Matrix Multiplication
Leetcode 311: Sparse Matrix Multiplication
311. Sparse Matrix Multiplication - Medium
[LeetCode] 311. Sparse Matrix Multiplication 稀疏矩阵相乘