[LeetCode] Sparse Matrix Multiplication
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的更多相关文章
- [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 ...
- [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 ...
- 311. Sparse Matrix Multiplication
题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- LeetCode 311. Sparse Matrix Multiplication
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...
- 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
- Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- [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 ...
- [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 ...
随机推荐
- 分布式数据库 HBase
原文地址:http://www.oschina.net/p/hbase/ HBase 概念 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用 ...
- CBA 赛程的笔记 - 北京首钢
2014-11-01 19:35 北京首钢 103:89 广东宏远 结束 技术统计 发挥不错,打的比较好! 2014-11-05 19:35 八一双鹿 89:100 北京首钢 结束 技术统计 第一 ...
- vim和tmux主题颜色不一致问题
没开tmux时使用vim 以及 开了tmux后使用vim 主题颜色不一致.随便打开一个.py文件,发现着色较深,非常影响阅读. 开始在.tmux.conf 中设置set -g defaul ...
- chosen PersistenceUnitInfo does not specify a provider class name
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceI ...
- Cacti学习笔记一:基本安装和配置
1.安装依赖包 yum -y install net-snmp-devel mysql mysql-devel openssl-devel libtool 2.安装RRDTool yum -y ins ...
- iOS开发-- 开发环境,证书和授权文件
一.成员介绍 1. Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1) Developer Certification(开发证书)安装在电脑 ...
- JQ例子:旋转木马
使用JQ现实旋转木马超级简单,它看起来很复杂,动画好像很难实现,但其实不然. 效果图: <!DOCTYPE html> <html lang="en"> & ...
- Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...
- Avizo应用 - Home和Set Home
Avizo的数据展示区域中两个选项Home和Set Home,如下图: 接下来会通过一套岩心的数据处理,解释一下这两个选项的一个用处. 首先这个数据已经完成了过滤处理,体渲染效果如下: 然后进行数据分 ...
- Selenium 3 -how to locate the chromedriver and geckodriver place?
Maybe you met these exceptions sometimes: 1. Chrome Driver The path to the driver executable must be ...