原题链接在这里: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. java实现rabbitMQ消息收发方式

    定义:消息队列(MQ)是一种应用程序对应用程序的通信方法是AMQP协议. jar包依赖: <!-- 加入mq消息依赖包 -->  <dependency>         &l ...

  2. python基础_MySQL的bigint类型

    bigint支持的数字的大小范围为:19位,存电话号码.有符号范围:-9223372036854775808 到 9223372036854775807 int支持的数字范围为:10位,有符号范围:- ...

  3. golang---获取windows系统相关信息

    package main import ( "fmt" "net" "runtime" "strings" " ...

  4. Java JDK1.8源码学习之路 1 Object

    写在最前 对于一个合格的后端程序员来说,现行的流行框架早已经能胜任基本的企业开发,Springboot 任何的框架都把重复的工作更佳简单/优化的解决掉,但是完全陷入在这样的温水里面, 好比温水煮青蛙, ...

  5. JDBC使用8.0驱动包连接mysql设置时区serverTimezone

    驱动包用的是新版 mysql-connector-java-8.0.16.jar新版的驱动类改成了com.mysql.cj.jdbc.Driver新版驱动连接url也有所改动I.指定时区 如果不设置时 ...

  6. asp.net core EF code first

    以连接mysql数据库为例 一 安装组件 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Relational Microsof ...

  7. webpack+vue搭建vue项目

    阅读地址: https://www.jianshu.com/p/23beadfa4aa5 源码地址:https://github.com/Ezoio/IMI-SOURCE-CODE

  8. 那些年,Linus torvalds大神喷过的技术

    Linus Torvalds 被认为是最伟大的程序员之一,因为他写出了使用最广泛的软件,如 Linux 内核和 Git 版本控制系统.但是他这个人经常因为讲话带有情绪,甚至是因为带有脏话的意见而饱受非 ...

  9. iOS进阶之多线程--NSThread详解

    NSThread简介 NSThread是苹果官方提供面向对象操作线程的技术,简单方便,可以直接操作线程对象,不过需要自己控制线程的生命周期.在平时使用很少,最常用到的无非就是 [NSThread cu ...

  10. 使用python库relate搭建LMS学习管理系统

    Relate is an Environment for Learning And TEaching Relate是在 Django上面构建的,可以快速搭建LMS系统,该系统可以方便学习管理和在线课程 ...