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:

A = [
[ 1, 0, 0],
[-1, 0, 3]
] B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
] | 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
 1 public class Solution {
2 public int[][] multiply(int[][] A, int[][] B) {
3 int m = A.length, n = A[0].length, nB = B[0].length;
4 int[][] C = new int[m][nB];
5
6 for (int row = 0; row < m; row++) {
7 for (int col = 0; col < n; col++) {
8 if (A[row][col] != 0) {
9 for (int j = 0; j < nB; j++) {
10 if (B[col][j] != 0) {
11 C[row][j] += A[row][col] * B[col][j];
12 }
13 }
14 }
15 }
16 }
17 return C;
18 }
19 }
 public class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if (A == null || A[] == null || B == null || B[] == null) return null;
int rowA = A.length, colA = A[].length, colB = B[].length;
int[][] C = new int[rowA][colB]; //Map<row, Map<col, val>>
Map<Integer, Map<Integer, Integer>> tableB = new HashMap<>(); // For each row in matrix B, if there is a non-zero value, put it in the hashMap.
for (int row = ; row < colA; row++) {
tableB.put(row, new HashMap<Integer, Integer>());
for (int col = ; col < colB; col++) {
if (B[row][col] != ) {
tableB.get(row).put(col, B[row][col]);
}
}
} for (int row = ; row < rowA; row++) {
for (int col = ; col < colA; col++) {
if (A[row][col] != ) {
for (Integer j : tableB.get(col).keySet()) {
C[row][j] += A[row][col] * tableB.get(col).get(j);
}
}
}
}
return C;
}
}

Sparse Matrix Multiplication的更多相关文章

  1. 311. Sparse Matrix Multiplication

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

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

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

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

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

  5. [LeetCode] Sparse Matrix Multiplication

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

  6. [Locked] Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

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

  8. LeetCode 311. Sparse Matrix Multiplication

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

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

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

随机推荐

  1. safari 调用隐藏fileInput

    在safari上,用自定义按钮调用隐藏fileInput,注意点 1. event listener中,不要 return false2. 不要使用display:none,可使用 opacity:0 ...

  2. CentOS7挂载分区教程

    http://www.centoscn.com/CentOS/config/2014/1016/3955.html

  3. ehcache memcache redis 三大缓存男高音

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  4. 自定义MapReduce的类型

    package org.apache.hadoop.mapreduce.io; import java.io.DataInput; import java.io.DataOutput; import ...

  5. css3动画由浅入深总结

    阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...

  6. VTK初学一,a Mesh from vtkImageData—球冠

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  7. 【Solr】数据库数据导入索引库

    目录 分析框图 配置数据库与solrconfig.xml 回到顶部 分析框图 框图画的粗糙!勿喷啊!勿喷啊! 回到顶部 配置数据库与solrconfig.xml Dataimport插件 可以批量把数 ...

  8. UI第一节—— UILable

    1.首先说说怎么创建UI程序,打开xcode,选择Create  a new Xcode project.看如下截图 2,接下来就蹦出一个和写OC应用差不多的界面,不多解释了 3.我给工程取得名字就叫 ...

  9. 学习javascript系列之变量

    在javascript全局变量中,未加var声明的全局变量和加上var声明的全局变量是不同的,虽然都是window对象的属性. ; window.a //1 delete a //false; 通过v ...

  10. 支付宝Payto接口的C#.net实现方法

    例一: using System; using System.Data; using System.Configuration; using System.Collections; using Sys ...