[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 ...
随机推荐
- THE BOX MODEL
Review In this lesson, we covered the four properties of the box model: height and width, padding, b ...
- 计算机&通信词典
目录 A B C Cgroups D E F G H I J K L M N NFV NFV ISG O ONF P Q R Rewrite S T U V VNFI W X Y Z A B C Cg ...
- Learn English like a Baby – How to Sound Native
Learn English like a Baby – How to Sound Native Share Tweet Share Tagged With: tips & tricks Wha ...
- ORACLE表空间操作实例
本文主要介绍oracle表空间常见的操作实例,包括创建.查询.增加.删除.修改.表空间和数据文件常用的数据字典和动态性能视图包括v$dbfile.v$datafile.v$tempfile.dba_s ...
- numpy.distutils.system_info.NotFoundError: no lapack/blas resources found问题解决
操作环境 Python3.6 + Windows7 问题现象 利用pip自动安装seaborn/numpy/scipy(pip install seaborn)模块失败,提示numpy.distu ...
- Assetbundle创建与加载
[Assetbundle创建与加载] Unity有两种动态加载机制:一种是Resource.Load.一种是AssetBundle.Assetbundle是Unity Pro提供的功能,它可以把多个游 ...
- Linux部署项目
1 安装jdk 第一步:获取Linux系统中jdk安装包和tomcat安装包(后面要用,所以上传两个) 第二步:使用secureCRT客户端工具连到服务器 第三步:使用命令创建一个目录,作为软件的安装 ...
- ssh问题:ssh_exchange_identification: Connection closed by remote host
ssh问题:ssh_exchange_identification: Connection closed by remote host... 刚刚一个朋友告诉我SSH连接不上服务器了,重启电脑也不管用 ...
- 移动端引用echarts的折线图
移动端写一个图表引用echarts,highcharts插件,本次要找一个能够显示最新数据的折线图,最后只找到显示最大值: 找到echarts的实例:记一下个各功能. <!DOC ...
- php挖掘数据编码问题
if(json_encode($jkkey) == 'null'){//判断不是utf8会返回空 $jkkey=mb_convert_encoding($jkkey,'utf-8','gbk'); } ...