[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 ...
随机推荐
- 如何查看一个class文件是否正确
今天碰到了个问题,左思右想就是找不出问题,试验多个路径来解决问题,错误依旧. 然后我拿到了现场的包,一个很大的问题让我忽略了,这个class文件用反编译程序打不开(jd-gui.exe),非常神奇,但 ...
- C# WINFORM 打包数据库
实现效果:安装项目时直接附加数据库. 1.首先在需要部署的项目的解决方案资源管理器中新建一个安装项目 2.在安装项目的文件视图中,右键[应用程序文件夹]->[添加]->[项目输出] ...
- ArcGIS案例学习笔记2_1_学校选址适宜性分析
ArcGIS案例学习笔记2_1_学校选址适宜性分析 计划时间:第二天上午 目的:学校选址,适宜性分析 内容:栅格数据分析 教程:pdf page=323 数据:chapter8/ex1/教育,生活,土 ...
- ArcGIS案例学习笔记-批量裁剪地理模型
ArcGIS案例学习笔记-批量裁剪地理模型 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 功能:空间数据的批量裁剪 优点:1.批量裁剪:任意多个目标数据,去裁剪任意 ...
- C# 中文判断
原地址忘了,也是博客园的 /// <summary> /// 是否是 中文 /// </summary> /// <param name="CString&qu ...
- JQuery 基本知识
一.简介 JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, ...
- XML 可扩展标记语言
因 为XML实在是太重要了,而且被广泛应用!不论是数据存储,还是其他方面,如配置文件等.XML是一种对独立于任何编程语言的数据进行编码的机制.在数据 交换领域,正在变得非常流行!因为他的基于节点的存储 ...
- linux 内核根文件系统
参考: http://blog.csdn.net/guopeixin/article/details/5962482 http://www.yunweipai.com/archives/1184.ht ...
- 总是Eqw
1.投递总是Eqw状态 qstat -j job_ID #Eqw状态的job id qconf -sq all.q |grep host qconf -shgrp @allhosts
- python-ceilometerclient命令行(2)
命令行解析工具argparse argparse是python标准库中的模块,利用argparse,可以完成对命令行的参数定义.解析以及后续的处理.一个简单的例子: # coding:utf-8 im ...