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 |

分析:

  第1种,求解一般矩阵乘积的方法;第2种,根据稀疏矩阵的特性减少0*x的计算次数。

代码1:

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;
}
};

代码2:

待补充

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

  1. 311. Sparse Matrix Multiplication

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

  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. 稀疏矩阵乘法 · Sparse Matrix Multiplication

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

  4. Sparse Matrix Multiplication

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

  5. [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 ...

  6. [LeetCode] Sparse Matrix Multiplication

    Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...

  7. [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 ...

  8. LeetCode 311. Sparse Matrix Multiplication

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

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

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

随机推荐

  1. OUTPUT、Merge语句的使用

    新版本的数据库中增加了OUTPUT子句,这个很好用,详细的使用方式大家可以参考SQL的联机帮助文档.这里仅记录下常用的场景:在对数据库进行增删改的时候我们有时候需要记录日志(这里指将日志记录在DB中而 ...

  2. wpf 大控件 打印 将控件转成 xps格式 并分页打印

    //PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...

  3. Java环境的安装与配置

    Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...

  4. 玩转CSLA.NET小技巧系列二:使用WCF无法上传附件,提示413 Entity Too Large

    背景:由于系统需要展示图片,客户上传图片到本地客户端目录,然后在数据库中存储本地图片地址,和图片二进制数据 错误原因:我是使用CSLA的WCF服务,使用了数据门户,WCF协议使用的是wsHttpBin ...

  5. 关于C#的编译与执行

    每一种编程语言,要想执行,就必须要转换为目标操作系统能够理解的语言才能执行,这种语言叫做本机代码(native code).C#也是一样的,也要做这样的转换,但是它不是一处到位的,在.NET Fram ...

  6. CPU风扇故障导致自动关机

    今天在使用电脑时,突然自动关机,重启后过一段时间又自动关机,于是打开机箱后盖,插上电源观察各个部位运行情况,发现CPU风扇不转,判断问题就是由于CPU温度太高了.于是换个风扇,再开机情况就正常了.

  7. DedeCMS时间格式

    时间格式 {dede:field name='pubdate' function='strftime("%Y年%m月%d日 %H:%M:%S","@me")' ...

  8. CSS3 box-shadow(阴影使用)

    from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...

  9. python模块之hashlib加密

    40.加密模块:hashlib      1.           >>> import hashlib >>> ret1 = hashlib.md5()     ...

  10. 关于C#的委托

    作者  陈嘉栋(慕容小匹夫) 阅读目录 0x00 前言 0x01 从观察者模式说起 0x02 向Unity3D中的SendMessage和BroadcastMessage说拜拜 0x03 认识回调函数 ...