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: 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的更多相关文章
- 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 稀疏矩阵相乘
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- [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] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
- [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 ...
- [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 311. Sparse Matrix Multiplication
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...
- 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
随机推荐
- Simple colum formatting in Yii 2 GridView
A very important widget in the business apps development is the GridView control. In this post I wil ...
- python 占位符 %s Format
1.百分号方式 %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指定的key flags 可选,可供选择 ...
- QT读写ini配置文件
/********下面是写ini文件*************************/ //Qt中使用QSettings类读写ini文件 //QSettings构造函数的第一 ...
- php-fpm进程关闭与重启脚本详解(转)
先来理解一下什么是php-fpm PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将 ...
- verilog阻塞与非阻塞的初步理解(三)
下面这段源码是因为习惯不好,出现不正确波形的例子. module pwm_division(reset,clkin,clkout); input reset,clkin; output clkout; ...
- C# ManualResetEvent和AutoResetEvent 使用笔记
一.两者区别 1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程.需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne ...
- 【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net
nanosleep的精度与调度算法的关系 2011-12-03 13:05:17 分类: LINUX Heartwork前辈在我前一篇博文多线程条件下的计数器(2)中的回复中提到, na ...
- hadoop常见问题汇集
1 hadoop conf.addResource http://stackoverflow.com/questions/16017538/how-does-configuration-addreso ...
- Storm与Spark Streaming比较
前言spark与hadoop的比较我就不多说了,除了对硬件的要求稍高,spark应该是完胜hadoop(Map/Reduce)的.storm与spark都可以用于流计算,但storm对应的场景是毫秒级 ...
- php后台多用户权限组思路与实现程序代码
网站开发少不了有网站后台,有了后台自然要对用户有同角色来分配一下,特别是多用户系统的情况下,如我一个系统要有多个管理员,那么我这些管理要分成,编辑,友情连接,管理员等,那我们要有权限和角色分配,今天我 ...