[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 equal to B's row number.
Example:
Input: A = [
[ 1, 0, 0],
[-1, 0, 3]
] B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
] Output: | 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
注意:
搞清楚何谓matrix multiply:
一定要有A column 等于B row的特性才能进行matrix multiply
| 1 0 0 | | 0 0 | | 0 0 | // 1*7 + 0*0 + 0*0 = 7
AB = | -1 0 3 | x | 0 0 | = | -7 0 3 |
| 0 1 |
| 1 0 0 | | 7 0 | | 7 0 | // 1*0 + 0*0 + 0*0 = 0
AB = | -1 0 3 | x | 0 0 | = | -7 0 3 |
| 0 1 |
| 1 0 0 | | 7 0 | | 7 0 | // 1*0 + 0*0 + 0*1 = 0
AB = | -1 0 3 | x | 0 0 | = | -7 0 3 |
| 0 0 |
| 1 0 0 | | 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 | = | -7 0 3 | // -1*7 + 0*0 + 3*0 = -7
| 0 1 |
思路:
Brute Force: create product 2D matrix, iterate through it and calculate result for each position
Optimized: Use the information that matrix is sparse. Iterate through A and add the contribution of each number to the result matrix. If A[i][j] == 0, skip the calculation
代码:
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
int m = A.length, n = A[0].length;
int nB = B[0].length;
int [][] res = new int[m][nB]; for(int i = 0; i< m; i++){
for(int k = 0; k < n; k++){
if(A[i][k]!=0){ // use Sparse Matrix attributes
for(int j = 0; j < nB; j++){
if(B[k][j]!=0) res[i][j] += A[i][k] *B[k][j];
}
}
}
}
return res;
}
}
[leetcode]311. 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
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...
- 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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- HDU 4920 Matrix multiplication(矩阵相乘)
各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...
- [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] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
随机推荐
- 容器viewController添加或者删除子viewController
假设有一个viewControllerA,我们想在viewControllerA中添加viewControllerB,需要执行以下方法: [viewControllerA addChildViewCo ...
- Delphi 修改本地日期和时间
procedure TForm1.ModifySysdate(D: Double); var systemtime:Tsystemtime; DateTime:TDateTime; begin Set ...
- mysql InnoDB引擎支持hash索引吗
https://blog.csdn.net/doctor_who2004/article/details/77414742
- linux 3.10 gro的理解和改进
gro,将同一个flow的一定时间范围之内的skb进行合并,减少协议栈的消耗,用于收包性能提升.gro网上的资料很多,但是都很少谈到gro的改进,刚好身边有个同事也想改这块的内容, 所以将最近看的gr ...
- 【转】解决Eclipse中SVN版本信息不显示的问题
eclipse 中使用 svn 插件,原本正常,未作任何更改,最近几天突然eclipse 中查看文件时,文件后面的 版本号 . 文件的状态图标 等等都不见了.以为有插件冲突,卸载了好多其他的相关的插件 ...
- APP安全性测试总结--网上转载
移动APP安全测试 老鹰a0人评论7103人阅读2018-08-06 16:22:07 1 移动APP安全风险分析 1.1 安全威胁分析 安全威胁从三个不同环节进行划分, ...
- php 查看当前页中的post及get数据
file_put_contents("log1209.html",date('Y-m-d H:i:s ')."-----<br>",FILE_APP ...
- [重点]delphi 实现 根据给定的标题去《中国青年报》网上电子报数据中查找匹配的内容,并从该内容中取出引题、正题、副题、作者和正文。
项目要求:根据给定的标题去<中国青年报>网上电子报数据中查找匹配的内容,并从该内容中取出引题.正题.作者和正文. unit Unit1; interface uses Winapi.Win ...
- servlet(1)request常用方法
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息. req ...
- Winform 对话框
ColorDialog:显示可用颜色,以及用户可以自定义颜色的控件,以调色板对话框形式出现,可选择更改字体颜色 FolderBrowserDialog:显示一个对话框,提示用户选择文件夹 FontDi ...