原题链接在这里: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的更多相关文章

  1. [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 ...

  2. 311. Sparse Matrix Multiplication

    题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...

  3. 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...

  4. 稀疏矩阵乘法 · Sparse Matrix Multiplication

    [抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂 ...

  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] Sparse Matrix Multiplication

    Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that ...

  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. [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 ...

  9. [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 ...

随机推荐

  1. 什么是REST 、RESTful 、RESTful API?

    介绍 自从Roy Fielding博士在2000年他的博士论文中提出Rest(Representational State Transfer)风格的软件架构模式后,REST就基本上迅速取代了复杂而笨重 ...

  2. maven工程目录和类加载器ClassLoader的记录

    URL url = this.getClass().getClassLoader().getResource("/" + packageName.replaceAll(" ...

  3. 使用无图形界面启动Centos

    Centos有些时候我们是不需要图形界面的 centos默认安装成功后是有图形界面的,为了减少系统开销,有时候我们需要无图形界面启动linux(centos7) systemctl set-defau ...

  4. C++ 读取 MATLAB 的 .m 文件,并发送到 MATLAB 运行

    本代码是由「Microsoft Visual Studio 2015 Enterprise」编写. 想要了解更多 C++ 与 MATLAB 混合编程的知识,可以参考我的另一篇博客:C++ 与 MATL ...

  5. AVR单片机教程——数字输出

    从上一篇教程中我们了解到,按键与开关的输入本质上就是数字信号的读取.这一篇教程要讲的是,控制LED的原理是数字信号的输出.数字IO是单片机编程之有别于桌面编程的各项内容中最简单.最基础的. 在讲数字信 ...

  6. Spring MVC传输对象属性

    今天搬砖时遇到一个问题,前端使用JSP+form传输数据,后台使用Spring MVC接收,但是接收到的对象属性一直是null,找了好久才发现原因,代码如下 前端代码   后端代码   需要注意一点 ...

  7. ABP 基于DDD的.NET开发框架 学习(七)继承不同的service直接调用api的区别

    1.IApplicationService->IBaseService->具体IXXXService 具体XXXService->BaseService,具体IXXXService ...

  8. python 简单工厂模式

    abc 是抽象类模块abc.ABC 是继承抽象类  也可直接继承 (metaclass=ABCMeta)abc.abstractmethod 是定义抽象方法 简单工厂模式:通过接口创建对象,但不会暴露 ...

  9. title 有背景边框自适应 mobile

    固定宽度,固定高度,来写背景的高度.这样就能居中.

  10. error: ~/.vuerc may be outdated. Please delete it and re-run vue-cli in manual mode

    @vue/cli创建项目时报错, 解决: 删除之. 查看vue/cli版本号: npm view @vue/cli versions --json