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 ...
随机推荐
- 08-Maps
Maps map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值.Maps类似于python中的字典 Maps定义 maps的key值必须是可hash(就是不可 ...
- git使用代理加快下载
git -c http.proxy=socks5://127.0.0.1:1086 clone https://github.com/.../...
- [CodeChef-ANUDTQ] Dynamic Trees and Queries
类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了.注意sum和lastans是long long. #include<cstdio> #include<algorith ...
- [LOJ#3120][Luogu5401][CTS2019]珍珠(容斥+生成函数)
https://www.luogu.org/blog/user50971/solution-p5401 #include<cstdio> #include<algorithm> ...
- .net core使用ocelot---第八篇 Consul
简介 .net core使用ocelot---第一篇 简单使用 .net core使用ocelot---第二篇 身份验证使用 .net core使用ocelot---第三篇 日志记录 .net ...
- C# 使用代理实现方法过滤
一.为什么要进行方法过滤 一些情况下我们需要再方法调用前记录方法的调用时间和使用的参数,再调用后需要记录方法的结束时间和返回结果,当方法出现异常的时候,需要记录异常的堆栈和原因,这些都是与业务无关的代 ...
- python-django中使用事务以及小坑
django中使用事务 一.导入事务模块 from django.db import transaction 二.对相应的业务进行事务操作 方式一:为整个函数进行事务操作 @transaction.a ...
- 删除注册在Eureka的服务(无效,多余)
例子:http://140.143.67.146:8000/eureka/eureka/apps/ZKR-PRODUCT/172.21.0.10:zkr-product:6868 Request Me ...
- 第五周(web,machine learning笔记)
2019/11/2 1. 表现层状态转换(REST, representational state transfer.)一种万维网软件架构风格,目的是便于不同软件/程序在网络(例如互联网)中互相 ...
- 重置文件reset
body { margin:0; padding:0; font-family: Helvetica, STHeiti, Droid Sans Fallback; // font-family: '微 ...