LeetCode 311. Sparse Matrix Multiplication
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/
题目:
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 |
题解:
按照两个矩阵相乘的公式计算结果.
Since It is spase, skip k loop when A[i][j] == 0.
Time Complexity: O(m*n*o). m = A.length, n = A[0].length, o = B[0].length.
Space: O(1). regardless res.
AC Java:
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
int m = A.length;
int n = A[0].length;
int o = B[0].length;
int [][] res = new int[m][o];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(A[i][j] != 0){
for(int k = 0; k<o; k++){
res[i][k] += A[i][j]*B[j][k];
}
}
}
}
return res;
}
}
LeetCode 311. Sparse Matrix Multiplication的更多相关文章
- [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 ...
- 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...
- 稀疏矩阵乘法 · Sparse Matrix Multiplication
[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...
- [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] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...
- Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- [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 ...
- [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 ...
随机推荐
- linux 下用C实现 ATM 自动取款机功能 (进程间通信)
直接先上图: 项目需求: 主要分为两人大模块: 客户端 .进入时的功能开户.销户.登录.解锁 开户:输入姓名.身份证号.设置密码,如果开户成功,则服务器上保存一个账号信号(一个账号存一个文件,文件名建 ...
- 中国大学MOOC-翁恺-C语言程序设计习题集(一)
练习 02-0. 整数四则运算(10) 本题要求编写程序,计算2个正整数的和.差.积.商并输出.题目保证输入和输出全部在整型范围内. 输入格式: 输入在一行中给出2个正整数A和B. 输出格式: 在4行 ...
- SAS学习笔记57 template的管理
template查询 首先点击SAS Windows左上方查询框,输入“odst”或者“odstemplates”,如下所示: 然后点击enter键,进入查询的template文件夹,如下所示: 这里 ...
- postgres 序列
postgres序列(serial)和类型:https://www.cnblogs.com/alianbog/p/5654604.html 序列:https://www.cnblogs.com/mch ...
- 在safari下input的placeholder设置行高失效
在项目中遇到input的placeholder在safari下设置行高失效的问题,亲测 input{ width:250px; height:30px; line-height:30px; font- ...
- xcode 手动管理内存 的相关知识点总结
一.XCode4.2以后支持自动释放内存ARC xcode自4.2以后就支持自动释放内存了,但有时我们还是想手动管理内存,这如何处理呢. 很简单,想要取消自动释放,只要在 Build Setting ...
- Air for ANE:打包注意的地方
来源:http://blog.csdn.net/hero82748274/article/details/8631982 今天遇到了一个打包ANE 文件的问题,导致花费了几个小时查找,最后师弟的一句话 ...
- Python学习日记(十五) collections模块
在内置函数(dict.list.set.tuple)的基础上,collections模块还提供了几个其他的数据类型:Counter.deque.defaultdict.namedtuple和Order ...
- PHP传引用/作用域 问题
$arr = [1,2,3]; foreach($arr as &$v) { //nothing todo. } foreach($arr as $v) { //nothing todo. } ...
- Qt命名规范
1) 类名:单词首字母大写,单词和单词之间直接连接,无需连接字符 如: MyClass,QPushButton class MainWindow { }; 2) 函数名字,变量名:第二个单词开始(不是 ...