稀疏矩阵乘法 · 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 ...
随机推荐
- Centos7 安装JDK环境和Tomcat
Linux JDK 64位下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.ht ...
- Eclipse 项目有红惊叹号
Eclipse 项目有红感叹号原因:显示红色感叹号是因为jar包的路径不对 解决:在项目上右击Build Path -> Configure Build Paht...(或Propertise- ...
- 创建目录mkdir
mkdir -p 在创建目录时,我们通常会先检查一下是否存在,如果不存在,就创建,这个时候通常用mkdir -p进行,但是-p是干什么用的呢. mkdir --help一下吧.也就说,如果上级目录不存 ...
- C#读写三菱Fx PLC 使用Fx 串口协议 读写Fx3U设备
本文将使用一个Github开源的组件库技术来读写三菱 FX PLC,使用的是基于串口的实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 github地 ...
- ZetCode PyQt4 tutorial signals and slots
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- DesignPattern(六)行为型模式(下)
状态模式 每个对象都有其对应的状态,而每个状态又对应一些相应的行为,如果某个对象有多个状态时,那么就会对应很多的行为.那么对这些状态的判断和根据状态完成的行为,就会导致多重条件语句,并且如果添加一种新 ...
- 设计模式(Python)-简单工厂,工厂方法和抽象工厂模式
本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...
- 【linux】Linux系统信息查看命令大全
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...
- Oracle本地网络服务名配置
1.安装Oracle 11G Client后可以在开始菜单中找到 选择NETCA->本地网络服务名配置 选择添加本地网服务名配置 这里的服务名:指的是也就是数据库名 在网络中架设C/S 客户端选 ...
- 在NOILINUX下的简易VIM配置
位置:/etc/vim/vimrc 建议使用gedit来进行配置.即使用命令:sudo gedit /etc/vim/vimrc set mouse=a " Enable mouse usa ...