Problem Description:

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 |

The solution is much more easier than we may have imagined. Take a look at this solution. But note that it is buggy as pointed out in the first comment (oh, how could the test cases be so weak)! I have rewritten it as follows.

class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
int m = A.size(), n = B.size(), p = B[].size();
vector<vector<int>> C(m, vector<int>(p, ));
for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (A[i][j])
for (int k = ; k < p; k++)
C[i][k] += A[i][j] * B[j][k];
return C;
}
};

[LeetCode] Sparse Matrix Multiplication的更多相关文章

  1. [LeetCode] Sparse Matrix Multiplication 稀疏矩阵相乘

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

  2. [leetcode]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 ...

  3. 311. Sparse Matrix Multiplication

    题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...

  4. 稀疏矩阵乘法 · Sparse Matrix Multiplication

    [抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...

  5. LeetCode 311. Sparse Matrix Multiplication

    原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...

  6. 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...

  7. Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

  8. [Swift]LeetCode311. 稀疏矩阵相乘 $ Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

  9. [Locked] Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

随机推荐

  1. Xperf Analysis Basics(转)

      FQ不易,转载 http://randomascii.wordpress.com/2011/08/23/xperf-analysis-basics/ I started writing a des ...

  2. javascript中的splice方法介绍&示例

    javascript 中的 splice 方法很强大,它可以用于插入.删除或替换数组的元素. 下面来一一介绍! 删除:用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数 ...

  3. Linux:常用快捷键

    按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂定当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,恢复到前台为命令fg Ctrl+a 将光标移至输入行头,相 ...

  4. [Android] Android Sutdio on Surface Pro 3

    Install Android Studio http://www.android-studio.org/index.php/download/androidstudio-download-baidu ...

  5. [BTS] EXCEPTION OBJECT_UNKNOWN RAISED

    Today, I generate a RFC schema, an error throwed by WCF-SAP adapter wazard. Microsoft.Adapters.SAP.R ...

  6. R 中同步进行的多组比较的包:npmc

    方差检验可以评估组间的差异.依据检验的结果,虽然你可以拒绝不存在差异的原假设,但方差检验并没有告诉你哪些组显著地与其他组有不同.Robert 在 <R in Action>一书中推荐了一个 ...

  7. WebLogic 12c SpringMVC Jackson 冲突 java.lang.NoSuchMethodError: TypeFactory.constructParametrizedType(Ljava/lang/Class;Ljava/lang/Class;[Ljava/lang/Class;)

    <?xml version="1.0" encoding="UTF-8"?> <wls:weblogic-web-app xmlns:wls= ...

  8. atitit. 解决org.hibernate.SessionException Session is closed

    atitit. 解决org.hibernate.SessionException Session is closed   #--现象:: org.hibernate.SessionException ...

  9. 第六章 应用层(DNS和http协议详解)

    序言 这是计算机网络基础的最后一篇博文了,大体的从物理层到最上层的应用层做了一个大概的了解,花了也有快1个月的时间了,在本章结尾会给你们我学习该课程的视频资料,我希望能帮到所有想学习想提高自己技术的同 ...

  10. Android兼容包multidex的开发和构建方法

    在Android开发中,函数方法超过65k限制后,我们就常常会用到multidex分包解决,但是multidex的配置,对系统apk的构建.签名.打包复杂性大大的增加,严重的降低了构建效率.那这个问题 ...