[抄题]:

给定两个 稀疏矩阵 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. MyEclipse web jsp 如何调试

    MyEclipse如何调试 | 浏览:882 | 更新:2014-03-13 17:38 1 2 3 4 5 分步阅读 当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行de ...

  2. python 读写西门子PLC 包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC

    本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 nu ...

  3. nginx配置文件的性能优化

    1.nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数.worker_processes 8; 2.为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或 ...

  4. .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  5. 企业web管理软件开发平台

    可以自定义拖动记忆位置的part桌面 权限(功能菜单查看范围)设置界面 权限(人员数据查看范围)设置界面 菜单排序和名称设置界面 业务模块 自定义字段界面 业务功能  列表界面 业务功能   查询界面 ...

  6. zz 跟风小结一下孕期~

    http://www.newsmth.net/nForum/#!article/FamilyLife/1754069968 发信人: milkcat (牛奶猫养了一只牛奶猫(*^__^*) 嘻嘻……) ...

  7. 有些文件不需要配置,只需要放到resources下面

    今天和一位同事探讨了一个问题,开始的时候我事先的读取某个映射文件是配置在applicationContext.xml的bean定义里面:但是他提出来,是否可以不需要配置呢?直接作为一个资源文件完事,这 ...

  8. apache make

    https://jingyan.baidu.com/article/7e4409533d7f0f2fc0e2ef91.html 1. apr apr-util http://archive.apach ...

  9. host is not allowed to connect to this mysql解决方案

    报错:1130-host ... is not allowed to connect to this MySql server   解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在l ...

  10. MySQL性能调优 – 你必须了解的15个重要变量

    1.DEFAULT_STORAGE_ENGINE 如果你已经在用MySQL 5.6或者5.7,并且你的数据表都是InnoDB,那么表示你已经设置好了.如果没有,确保把你的表转换为InnoDB并且设置d ...