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 稀疏矩阵相乘的更多相关文章

  1. [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 ...

  2. LeetCode 311. Sparse Matrix Multiplication

    原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...

  3. 311. Sparse Matrix Multiplication

    题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...

  4. 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...

  5. 稀疏矩阵乘法 · Sparse Matrix Multiplication

    [抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...

  6. HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  7. HDU 4920 Matrix multiplication(矩阵相乘)

    各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...

  8. [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 ...

  9. [LeetCode] Sparse Matrix Multiplication

    Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...

随机推荐

  1. Xcode 8 注释快捷键失效

    sudo /usr/libexec/xpccachectl 重启

  2. 红警2在Y460和win10下运行

    1.将电源模式改为高性能模式 2.以WinXP兼容模式运行 3.修改RA2.ini文件 在[Video]下加入以下代码 : AllowHiResModes=yes VideoBackBuffer=no ...

  3. 多线程(Java)

    Thread 类 和 Runnable 接口 在Java中实现多线程有两种方法 继承 Thread 类 优点:通过覆盖Thread 类的方法,可以改变线程的行为. 实现 Runnable 接口 优点: ...

  4. linux文件和目录的删除、新建、移动等操作

    在Linux下进行切换目录   cd 在Linux下查看当前目录下的内容   ls.  ll.  ls -al 如何显示当前命令所在的目录路径   pwd 在Linux下创建目录   mkdir 在L ...

  5. HTML+CSS基础课程三

    1.文字排版--字体 我们可以使用css样式为网页中的文字设置字体.字号.颜色等样式属性.下面我们来看一个例子,下面代码实现:为网页中的文字设置字体为宋体. body{font-family:&quo ...

  6. getElementsByTagName

    1.getElementsByTagName * document.getelementByID(id名称)* 通过id名称获取元素,它只有一个主语,docment(整个文档)** getElemne ...

  7. effective C++学习一(仅供个人学习记录,本文摘录effective C++)

    条款 1:尽量用 const 和 inline 而不用#define   #define ASPECT_RATIO 1.653 编译器会永远也看不到 ASPECT_RATIO 这个符号名,因为在源码进 ...

  8. EF 安装框架

    在NuGet中安装ef框架 命令:Install-package EntityFramework

  9. js高级-闭包

    function foo(x){ var tmp = 3; return function(y){ //把一个函数作为返回值,定义时候的作用域 console.log(x+y+(++tmp)) //+ ...

  10. 微信小程序--分享报错(thirdScriptError Cannot read property 'from' of undefined;at pages/index/index page onShareAppMessage function TypeError: Cannot read property 'from' of undefined)

    分享功能: onShareAppMessage: function (res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res. ...