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 ...
随机推荐
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pat----------<base>元素有关
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. ...
- getAttribute和getParameter的区别
2016年1月19日JSP中getParameter与getAttribute有何区别? ——getParameter得到的都是String类型的.或者是http://a.jsp?id=123中的12 ...
- jar war
区别:Jar.war.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式.但是它们的使用目的有所区别: Jar文件(扩展名为. Jar,JavaApplication ...
- C++中const 的各种用法
C++中const 关键字的用法 const修饰变量 const 主要用于把一个对象转换成一个常量,例如: ; size = ; // error: assignment of read-only v ...
- yii2 Pjax的使用
有两个例子:刷新时间和数据显示排序 1.刷新时间 (1)控制器中的方法:Time public function actionTime() { return $this->render('tim ...
- 使用 Elasticsearch ik分词实现同义词搜索(转)
1.首先需要安装好Elasticsearch 和elasticsearch-analysis-ik分词器 2.配置ik同义词 Elasticsearch 自带一个名为 synonym 的同义词 fil ...
- mysql配置mysql-proxy读写分离
MySQL配置读写分离 192.168.23.131与192.168.23.132两台服务器,131是主,132是从,131是读写,132是只读.myql-proxy的IP是192.168.23.13 ...
- 【10-25】OOP基础-飞机游戏知识点
知识点 鼠标适配器类为抽象类,使用匿名子类实现鼠标事件的重写,创建一个鼠标适配器对象 添加鼠标事件监听器到JPanel对象实现鼠标的响应 创建定时器Timer对象,在定时器的任务列表方法schedul ...
- the usage of linux command "expect"
#! /usr/bin/expect -f# this script is used to practise the command "expect" #when "li ...
- css3动画由浅入深总结
阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...