三刷311. Sparse Matrix Multiplication

L medium
已经很熟了 思路很直接 看清楚每行每列是什么就行

class Solution {
    public int[][] multiply(int[][] A, int[][] B) {
        int n = A.length;
        int m = B[0].length;
        int s = A[0].length;
        int[][] C = new int[n][m];
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                //   (k = 0 -> s)
                //C[i][j] = A[i][k]*B[k][j]
                for (int k = 0; k < s; k++){
                    if (A[i][k] == 0 || B[k][j] == 0){
                        continue;
                    }
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        return C;
    }
}

你可能感兴趣的:(三刷311. Sparse Matrix Multiplication)