[抄题]:

给定两个 稀疏矩阵 A 和 B,返回AB的结果。
您可以假设A的列数等于B的行数。

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

[一句话思路]:

如果为零则不相乘,优化常数的复杂度。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 是ans[i][j] 元素变化来进行具体的加减操作,而不是无参数的ans[][] 用来声明,搞混了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

int[][] ans = new int[m][n]二元数组要给引用分配空间

[复杂度]:Time complexity: O(n^3-n^2*c) Space complexity: O(m*n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param A: a sparse matrix
* @param B: a sparse matrix
* @return: the result of A * B
*/
public int[][] multiply(int[][] A, int[][] B) {
int m = A.length;
int n = B[0].length;
int[][] ans = new int[m][n];
//corner case
if (A == null || A[0] == null || B == null || B[0] == null) {
return ans;
}
//loop
for (int i = 0; i < A.length; i++) {
for (int k = 0; k < B.length; k++) {
if (A[i][k] == 0) {
continue;
}
for (int j = 0; j < B[0].length; j++) {
ans[i][j] += A[i][k] * B[k][j];//
}
}
}
return ans;
}
}

稀疏矩阵乘法 · Sparse Matrix Multiplication的更多相关文章

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

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

  3. 311. Sparse Matrix Multiplication

    题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...

  4. matlab 稀疏矩阵(sparse matrix)

    参数的设置:spparms() spparms('spumoni', 3);:Set sparse monitor flag to obtain diagnostic output 1. 创建稀疏矩阵 ...

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

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

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

  7. Sparse Matrix Multiplication

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

  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. [LeetCode] Sparse Matrix Multiplication

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

随机推荐

  1. HTML5和XHTML的区别

    既然被问到了HTML5和XHTML的区别,那我就在这里给大家分享一些我个人的理解,同时我也觉得从他们的来源上讲,他们藏着一个有趣的故事. 首先认识三个组织,IETF (Internet Enginee ...

  2. Openlayers4中地图的导出

    概述: 本文讲述Openlayers4中地图的导出,包括调用天地图切片跨域.Geoserver11 WMS跨域等. 效果: 导出图片 页面展示 实现代码: document.getElementByI ...

  3. 20145237 Exp9 Web安全基础实践

    基础问题回答 SQL注入攻击原理,如何防御: 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,黑客利用这个bug在数据输入区恶意填入脚本,当数据被传回后台,黑客所填入的脚本语句被运行, ...

  4. java spring boot 出现 java.lang.UnsatisfiedLinkError

    java.lang.UnsatisfiedLinkError: E:\ruanjian\jdk\bin\tcnative-1.dll: Can't load IA 32-bit .dll on a A ...

  5. FastAdmin 开发时如何与官方同步升级

    FastAdmin 开发时如何与官方同步升级 使用 FastAdmin 开发时为了与官方同步升级,推荐使用 git 管理代码. 官网上提供的完整包是为了方便第一次使用的人快速测试. 我一般是给官方的 ...

  6. tasks

    Edit: F:\wamp\www\tasks Task ID Name Links? Date commit Date Done 9 Read openCV documents F:\wamp\ww ...

  7. 我的第一个php扩展

    一.进入php源码包,找到ext文件夹 cd /owndata/software/php-5.4.13/ext 文件夹下放的都是php的相关扩展模块 二.生成自己的扩展文件夹和相关文件 php支持开发 ...

  8. mysql中去重 distinct 用法

    在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...

  9. 黄聪:VS2010启动程序提示文件加载 使用 简体中文(GB2312)编码加载文件解决办法

    vs2010 错误提示框:文件加载 使用 简体中文(GB2312)编码加载文件C:\Users\Administrator\AppData\Local\Temp\nxhgjasi.5au \Temp\ ...

  10. python3_爬虫_爬百度音乐

    工具及环境 1.操作系统:windows 64位系统 2.软件工具:谷歌浏览器.pycharm集成开发工具 3.第三方库:request 注:如果第三方库搭建有困难,请看博客:https://www. ...