Sparse Matrix Multiplication

https://leetcode.com/problems/sparse-matrix-multiplication/

class Solution {
public:
    vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
        int rowa = A.size();
        int cola = A[0].size();
        int colb = B[0].size();

        vector<vector<int>> result(rowa, vector<int>(colb, 0));

        for(int i = 0; i<rowa; i++)
        {
            for(int k = 0; k<cola; k++)
            {
                if(A[i][k] == 0)
                {
                    continue;
                }
                for(int j = 0; j<colb; j++)
                {
                    result[i][j] += A[i][k]*B[k][j];
                }
            }
        }

        return result;
    }
};

results matching ""

    No results matching ""