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 ...
随机推荐
- 移植net-snmp到开发板(mini210)
1.安装交叉编译工具arm-linux-gcc 2.下载net-snmp源码安装包 3.解压安装包 4../configure --build=i686-linux --host=arm-linux ...
- String、StringBuilder、StringBuffer
String String ...
- 四则运算出题器(c++)
一.设计思路 这次版本加入了一下功能: 可定制题目的数量:修改循环次数: 可以定制每行打印的题目数和行间距的大小(当前题目序号可以整除定制数时输出输入的行间距个换行符): 可以定制算式的范围(修改随机 ...
- js基础知识点(只有点)
转自:2015年12月的文章 http://blog.csdn.net/u014326381/article/details/50176339 JavaScript: 作用域链.闭包.运行时上下文.t ...
- Careercup - Microsoft面试题 - 5649647234187264
2014-05-10 22:17 题目链接 原题: A draw method is given, write a function to draw a chess board. The Draw m ...
- android开发 drawtext的开始坐标位置
我们canvas绘制文字的遇到一个不知道drawtext(str,x,y,paint) 中的x.y坐标值怎么定义,,如果设为(0,0)的话文字就不会出来了.因此查找到一下资料: 问:canvas.d ...
- 在一台电脑访问另一台电脑的mysql数据库,并增加和剥夺权限
1. 假设MySQL服务器安装在ip地址为192.168.105.3的主机上面 2. 再假设客户端安装在ip为192.168.105.100的机子上 3. 首先在ip为192.168.10 ...
- 计算器软件的代码实现 (策略模式+asp.net)
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- JS 学习笔记--7---正则表达式
正则表达式中的内容很多,也很深,下面只是一些基本的知识点,练习中使用的浏览器是IE10,若有不当处请各位朋友指正,我会在第一时间修改错误之处. 匹配的概念:是包含的意思,不是相等的意思 1.正则表达式 ...
- 【BZOJ】【TJOI2015】线性代数
网络流/最小割/最大权闭合图 2333好开心,除了一开始把$500^2$算成25000……导致数组没开够RE了一发,可以算是一次AC~ 咳咳还是回归正题来说题解吧: 一拿到这道题,我就想:这是什么鬼玩 ...