题目:

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的更多相关文章

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

  2. LeetCode 311. Sparse Matrix Multiplication

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

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

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

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

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

  5. Sparse Matrix Multiplication

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

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

  7. [LeetCode] Sparse Matrix Multiplication

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

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

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

随机推荐

  1. Cocos2dx中利用双向链表实现无限循环滚动层

    [Qboy原创] 在Cocos2dX 3.0 中已经实现一些牛逼的滚动层,但是对于有一些需要实现循环滚动的要求确没有实现,笔者在前段时间的一个做了一个游戏,需求是实现在少有的(13个)英雄中进行循环滚 ...

  2. DataTemplate和ControlTemplate联系与区别

    ---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...

  3. 时间日期设置--ctime头文件

    简述:ctime头文件中的4中与时间相关的数据类型 <ctime>头文件中定义了4种与时间有关的数据类型,如下:clock_tsize_ttime_tstruct tm clock_tCl ...

  4. 常用EXE文件反编译工具

    PE Explorer V1.99 R5 绿色汉化特别版_强大的可视化汉化集成工具 功能极为强大的可视化汉化集成工具,可直接浏览.修改软件资源,包括菜单.对话框.字符串表等: 另外,还具备有 W32D ...

  5. 在一台电脑访问另一台电脑的mysql数据库,并增加和剥夺权限

    1.    假设MySQL服务器安装在ip地址为192.168.105.3的主机上面 2.    再假设客户端安装在ip为192.168.105.100的机子上 3. 首先在ip为192.168.10 ...

  6. block extends include三者的差别跟用法

    block extends include三者的差别跟用法 一.定义基础模板,在html内容中定义多个block块,block由子模板引用同名block块,来决定是否替换这些部分{% block ti ...

  7. AngularJS打印问题

    http://stackoverflow.com/questions/22189544/print-a-div-using-javascript-in-angularjs-single-page-ap ...

  8. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  9. C# TcpListener的编程要点

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  10. 在JavaScript中判断整型的N种方法

    原文:http://www.cnblogs.com/YcYYcY/p/3759184.html 整数类型(Integer)在JavaScript经常会导致一些奇怪的问题.在ECMAScript的规范中 ...