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 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 |
链接: http://leetcode.com/problems/sparse-matrix-multiplication/
题解:
Sparse Matrix相乘。题目提示要用HashMap,于是我们就用HashMap, 保存A中不为0行,以及B中不为0的列,然后遍历两个hashmap来更新结果数组。
Time Complexity - O(mnkl), Space Complexity - O(mn + kl)。
public class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if(A == null || B == null || A.length == 0 || B.length == 0 || (A[0].length != B.length)) {
return new int[][]{};
}
Map<Integer, int[]> rowInA = new HashMap<>(); // store non-zero rows in A
Map<Integer, int[]> colInB = new HashMap<>(); // store non-zero cols in B
for(int i = 0; i < A.length; i++) {
for(int j = 0; j < A[0].length; j++) {
if(A[i][j] != 0) {
rowInA.put(i, A[i]);
break;
}
}
}
for(int j = 0; j < B[0].length; j++) {
for(int i = 0; i < B.length; i++) {
if(B[i][j] != 0) {
int[] tmp = new int[B.length];
for(int k = 0; k < B.length; k++) {
tmp[k] = B[k][j];
}
colInB.put(j, tmp);
break;
}
}
}
int[][] res = new int[A.length][B[0].length];
for(int i : rowInA.keySet()) {
for(int j : colInB.keySet()) {
for(int k = 0; k < A[0].length; k++) {
res[i][j] += rowInA.get(i)[k] * colInB.get(j)[k];
}
}
}
return res;
}
}
Reference:
311. Sparse Matrix Multiplication的更多相关文章
- [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 ...
- 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 ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- 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 稀疏矩阵相乘
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 ...
随机推荐
- JNA开发中的问题积累
[Qboy原创] 2013年12月28日 在开发一个项目过程中需要调用第三方的C的dll.由于是第一次在项目中使用JNA,很多都安装开发文档来做,但是出现了很多的问题. 由于很多接口还没调完,还不知道 ...
- XAML中ContentControl,ItemsControl,DataTemplate之间的联系和区别
接触XAML很久了,但一直没有深入学习.今天学习了如标题所示的内容,所以来和大家分享一下,或者准确的说是自我回顾一遍. 在XAML中,有两类我们常见的控件,分别是ContentControl和Item ...
- mysql 触发器的使用(备忘)
触发器创建语法四要素: 1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/del ...
- Careercup - Google面试题 - 6407924087783424
2014-05-07 15:17 题目链接 原题: Given an array of n elements (a1,a2,..ai,...,an). You are allow to chose a ...
- android开发实现静默安装(fota升级)
这里只提供一个思路,也是咨询大神才了解到的. fota升级主要用于系统及系统应用的升级,不过貌似也会弹出提示用于用户确认.既然做到系统级别了,估计也一样可以静默安装的.
- 【数据结构】通用的最小堆(最大堆)D-ary Heap
听说有一种最小(大)堆,不限于是完全二叉树,而是完全D叉树,名为D-ary Heap(http://en.wikipedia.org/wiki/D-ary_heap).D可以是1,2,3,4,100, ...
- 【BZOJ】【2245】【SDOI2011】工作安排
网络流/费用流 裸题吧……直接建模就好了……所谓的“分段函数”就是吓唬你的,其实就是对于每个人分开建几条流量不同.费用不同的弧而已. 对每种产品,连S->i ,(c[i],0):对每个工作人员 ...
- fpu栈溢出
老大们遇到个问题,有一堆浮点数运算,分开写就对,合一起就溢出. 是因为定义的函数返回float的时候,别的地方声明是void错了,这样的错误累计八次之后,浮点数寄存器就满了.没地方放就错了. 函数前面 ...
- 如何撰写SCI论文的讨论部分?——经典结构 – 俗称“倒漏斗型。
- kerberos+ladp+hadoop-ha 安全认证部署配置
随着hadoop集群里的数据量越来越大,各业务数据都集中了里面,自然要为各业务都要提供数据支持,又希望各业务数据是相对独立安全的,这最时候就需要做安全认证了 hadoop ha 测试集群部署规划 ha ...