稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]:
给定两个 稀疏矩阵 A 和 B,返回AB的结果。
您可以假设A的列数等于B的行数。
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
[一句话思路]:
如果为零则不相乘,优化常数的复杂度。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 是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的更多相关文章
- [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 ...
- [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 ...
- 311. Sparse Matrix Multiplication
题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...
- matlab 稀疏矩阵(sparse matrix)
参数的设置:spparms() spparms('spumoni', 3);:Set sparse monitor flag to obtain diagnostic output 1. 创建稀疏矩阵 ...
- [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】311. Sparse Matrix Multiplication 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
- Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- [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 ...
- [LeetCode] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
随机推荐
- MAC 下编译 ANDROID P 源码 提示 internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12" "10.13"]
MAC 下编译 ANDROID P 源码出现下面的问题: ninja: no work to do. [21/21] out/soong/.bootstrap/bin/soong_build out/ ...
- Zabbix监控TCP status
监控原理 ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' LAST-ACK 5ESTAB 348FIN-WAIT-1 ...
- Swift 导航栏设置
let width = UIScreen.mainScreen().bounds.size.width let height = UIScreen.mainScreen().bounds.size.h ...
- c#:Json字符串转成xml对象
没看到.net framework中有这样的功能, 懒得到处找了, 索性花点时间自己写一个 /* * Created by SharpDevelop. * Date: 2013/6/24 * User ...
- centos6.6 myphpadmin
基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...
- 360全景技术支持中心(KRPanoGUI三维全景制作软件)
http://www.360pano.cn/ http://www.360pano.cn/88/ http://www.suse.edu.cn/qjmy/hd/index.html
- 【并发编程】Future和FutureTask以及CompletionService
Future接口 此接口主要用于: 代表异步计算的执行结果: 用于可取消的task:(比使用interrupt实现取消要方便 ) FutureTask类 FutureTask是Future的一个实现类 ...
- py基础3--函数,递归,内置函数
本节内容 函数基本语法及特性 参数与局部变量 返回值 嵌套函数 递归 匿名函数 函数式编程介绍 高阶函数 内置函数 1. 函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况 ...
- Codeforces Round #474-E(树形dp)
一.题目链接 http://codeforces.com/contest/960/problem/B 二.题意 给定一棵$N$个节点的树,每个节点的权值$V$.定义树中两点$u_1$和$u_m$的权值 ...
- mybatis“$”和“#”
摘要:$ 是直接拼接# 会转义,更安全 类比Mybatis的执行流程和JDBC原有的我们使用的方法就是:Mybatis: Sqlsession -> Executor -> Stateme ...