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. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  2. 为什么java实体类需要重写toString方法

    如果没重写toString的情况: Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话 ...

  3. Notepad++好用的功能和插件

    Notepad++是一款Windows环境下免费开源的代码编辑器,支持Python,shell,Java等主流语言编写.本文主要描述Notepad++一些好用但是容易忽视的功能. 1.根据文件内容查找 ...

  4. LeetCode OJ 93. Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  5. adb INSTALL_FAILED_UPDATE_INCOMPATIBLE

    今天用Eclipse运行项目时出错: LOG: [2018-05-09 14:16:19 - Module_Android_Demo] ------------------------------ [ ...

  6. 吴裕雄 oracle 管理数据表对象

  7. pycahrm 激活

    Linux在/etc/hosts中添加 0.0.0.0 account.jetbrains.com就好,直接添加:0.0.0.0 account.jetbrains.comwindows的话没记错应该 ...

  8. 基础的正则表达式与re模块(2)

    一.元字符 字符组是元字符中的一个.在字符组中所有的字符都可以匹配任意一个字符位置上能出现的内容,如果在字符串中有任意一个字符是字符组中的内容,那么就是匹配上的项. [0-9]   [a-z]    ...

  9. ASP.NET 散碎知识

    1.按钮点击打开一个新的Web窗体,可在按钮点击事件里面写:Response.Redirect("窗体的名字.aspx"); 2.复合控件: CheckBoxList - 复选框组 ...

  10. Mybatis之mapper.xml配置文件中的#{}和${}

    #{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换.#{}可以有效防止sql注入. #{}可以接收简单类型值或pojo ...