[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 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的更多相关文章
- 311. Sparse Matrix Multiplication
题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...
- [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 ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- 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] 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] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
- [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 ...
- 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 ...
随机推荐
- ASP.NET 3.5路由总结篇
URL Routing是非常重要的一块技术体系,笔者将URL Routing的知识进行梳理后得出本文,旨在同大家分享,希望能够起到抛砖引玉的作用. 1. 什么是URL Routing? 所谓UR ...
- apache httpd配置ajp报错:ap_proxy_connect_backend disabling worker for (localhost)
报错信息: (13)Permission denied: proxy: AJP: attempt to connect to 127.0.0.1:9019 (localhost) failed[Wed ...
- 谈谈oracle中的临时表
--------------------创建临时表 临时保存从xml字符串解析来的数据--------------------------- 会话级别临时表SQL> create global ...
- 【转】 iOS KVO KVC
原文: http://www.cocoachina.com/industry/20140224/7866.html Key Value Coding Key Value Coding是cocoa的一个 ...
- 解决UITableView中Cell重用机制导致内容出错的方法总结
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- iOS RC4加解密算法
-(NSString *)encrypt:(NSString *)string withKey:(NSString *)key{ self.sBox = [[self frameSBox:key] m ...
- ID选择器
在很多方面,ID选择器都类似于类选择符,但也有一些重要的区别: 1.为标签设置id="ID名称",而不是class="类名称". 2.ID选择符的前面是井号(# ...
- wordpress 当前栏目名,当前栏目的分类名
wordpress在设计主题和做模板时经常会用到调用当前分类栏目名称,常见的有当前栏目页.文章页,详情代码如下: 1.分类名称与链接 <?php the_category(); ?> 2. ...
- 【POJ2886】【线段树】Who Gets the Most Candies?
Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...
- 【POJ3580】【块状链表】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...