题目:

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. web 性能忧化(IIS篇)

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings 2.   ...

  2. 搭建SpringMVC+MyBatis开发框架一

    大部分 Java 应用都是 Web 应用,展现层是 Web 应用不可忽略的重要环节.Spring 为展现层提供了一个优秀的 Web 框架—— Spring MVC.和众多其他 Web 框架一样,它基于 ...

  3. GBDT(MART)

    转自:http://blog.csdn.net/w28971023/article/details/8240756 在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(Gra ...

  4. Ombrophobic Bovines - POJ 2391

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  5. SPOJ - DQUERY 主席树

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...

  6. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  7. Leetcode#137 Single Number II

    原题地址 遍历所有数字,统计每一位出现的次数,模3即为只出现一次的数字在那一位的情况. 代码: int singleNumber(int A[], int n) { ] = {}; ; i < ...

  8. BZOJ: 1084: [SCOI2005]最大子矩阵

    NICE 的DP 题,明白了题解真是不错. Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1228  Solved: 622[Submit][Stat ...

  9. ios7.1 在线安装 失败的解决办法

    昨天升级7.1之后,证书制作的app就无法在线安装了,各种搜..说需要https.搭web服务器,起https服务,结果不行.终于明白,https需要权威认证,自己搭建的不行,需要买第三方权威机构的认 ...

  10. DevSecOps 简介(一)

    DevOps,或者说企业应用开发团队和系统运营团队的合作,已经成为一个时髦的 IT 话题.这一新的运营模式往往与敏捷式软件开发方法并举,同时还会利用云计算的可扩展性--这一切,都是为了使企业更加灵活, ...