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. 在线预览文件(pdf)

    1.flash版(借助flexpaper工具) 可以把pdf文件用pdf2swf工具转换成swf文件.下载地址http://www.swftools.org/download.html 转换代码如下: ...

  2. Android开发中用友盟做分享的一些坑

    仅限于用5.1.4版本的 按照友盟分享的API在自己的代码中修改: 1.微信分享需要打包APK文件,数字签名与微信开发申请的要一致 2.此name中属性不能修改 value为友盟的申请的appkey ...

  3. oracle服务器端-登陆

    由于的的操作系统是windows server版本,所以想装服务器端的server版本,一般的oracle都有'scott'用户,但是貌似服务器端的没有该用户,我用以下方式登陆: sqlplus / ...

  4. Opencart 之 Registry 类详解

    Registry 中文意思是记录,登记,记录本的意思, 在opencart中他的用途就是 登记公共类.类的原型放在 system\engine文件夹下 代码很简单: <?php final cl ...

  5. GCD介绍(三): Dispatch Sources

    何为Dispatch Sources         简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue ...

  6. [转]Delphi I/O Errors

    The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...

  7. OpenCV2.4.9 & Visual Studio 2010 环境配置篇

    1. 准备工作 1.1. 安装 Visual Studio 2010, 需要安装 VC++ 相关功能.具体可求助度娘. 1.2. 下载 OpenCV 2.4.9 For Windows:https:/ ...

  8. websocket以及自定义协议编程一些总结

    以下仅供自己翻阅,因为时间久了会忘2.发送缓冲区主要是为了处理发送前一些小内容,可以自己控制flush,或者write的不是那么频繁因为没必要.至于大内容就没必要了.3.其实tcp以上的通信协议也好, ...

  9. 实例:jQuery实现标签切换

    具体实现效果如图: 原理很简单,就是监听鼠标滑动和点击事件.在第一个标签切换的示例中,当鼠标滑过某个标签时,就把class转移到当前标签.这里用到的jQuery方法主要是each()确定当前是哪一个标 ...

  10. PHPCMS get当中使用limit

    最近在用PHPCMS V9做一个站子,发现get标签非常好用,自定义模型后get几乎变成万能的了.但是PHPCMS升级到V9后,把2008的很多功能都去掉了,比如get标签中,在后面自动添加了一个LI ...