题目地址:https://leetcode-cn.com/problems/sparse-matrix-multiplication/

题目描述

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:

Input:

A = [
[ 1, 0, 0],
[-1, 0, 3]
] B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
] Output: | 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |

题目大意

给定两个 稀疏矩阵 A 和 B,请你返回 AB。你可以默认 A 的列数等于 B 的行数。

解题方法

暴力

直接按照矩阵乘法,可以暴力求解。矩阵乘法是三重循环,时间复杂度是O(N^3)。

题目给的系数矩阵的特征怎么用呢?可以考虑先遍历一次两个矩阵,记录下A的行和B的列全部为0的索引,当遍历到这些索引时,直接给res中填入0。下面的代码没有这么做,也能通过。

C++代码如下:

class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
if (A.empty() || A[0].empty() || B.empty() || B[0].empty()) return vector<vector<int>>();
int M = A.size();
int N = B[0].size();
vector<vector<int>> res(M, vector<int>(N, 0));
for (int row = 0; row < M; ++row) {
for (int col = 0; col < N; ++col) {
int cur = 0;
for (int i = 0; i < A[0].size(); ++i) {
cur += A[row][i] * B[i][col];
}
res[row][col] = cur;
}
}
return res;
}
};

科学计算库numpy

Python有科学计算库numpy可以使用,直接使用库函数求得矩阵的乘法。

import numpy as np
class Solution(object):
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
a = np.array(A)
b = np.array(B)
return np.matmul(a, b)

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)的更多相关文章

  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. LeetCode 311. Sparse Matrix Multiplication

    原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...

  3. 311. Sparse Matrix Multiplication

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

  4. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  5. zoj 2316 Matrix Multiplication 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2316 题目意思:有 N 个 点,M 条 边.需要构造一个N * ...

  6. 【LeetCode】498. Diagonal Traverse 解题报告(Python)

    [LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. Pacbio三代基因组组装简介

    参考: 视频PPT来自欧易生物讲座:如何开启一个动植物基因组三代de novo项目?

  2. GWAS初探

    原理 GWAS 的主要方法学依据是归纳法中的共变法,是探究复杂因果关系最主要的科学思维和方法.所谓共变法,是通过考察被研究现象发生变化的若干场合中,确定是否只有一个情况发生相应变化,如果是,那么这个发 ...

  3. python17进程

    import os import time from multiprocessing.dummy import Process def so_sth(name): print("进程名称{} ...

  4. 【Python小试】去除核酸特定长度的接头序列

    输入 input.txt ATTCGATTATAAGCTCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATC ATTCGATTATAAGCACTGATCGATCGATCG ...

  5. SQL-join(inner join)、left join、right join、full join

    0.JOIN 类型 有时我们需要从两个或更多的表中获取结果,数据库中的表可通过键将彼此联系起来.每个表中都有一个主键,主键(Primary Key)是一个列,值都唯一.这样做的目的是在不重复每个表中的 ...

  6. centos7.4 64位安装 git

    参考博客:Linux Jenkins配置Git 1. git --version 查看有没有安装 过 git,没有则 继续 2. git 压缩包下载地址:https://mirrors.edge.ke ...

  7. maven管理本地jar包

    maven作为包管理工具,好处不必多说.但是有些情况,比如需要引入第三方包,如快递鸟,支付宝,微信等jar包(当然有可能直接提供maven依赖),如果直接下载到本地之后,怎么整合到自己的maven工程 ...

  8. 如何使用pycharm克隆阿里云项目

    我们回到PyCharm刚打开时的界面,如图1-1所示:   点击"Check out from Version Control" => "Git",如图1 ...

  9. 如何利用EL表达式获取list,map,对象等值

    <%@ page import="com.hopetesting.domain.User" %><%@ page import="java.util.* ...

  10. 数据恢复binlog2sql

    目录 一.原理及其使用 用途 闪回原理简析 binlog 有三种可选的格式: 来实例演习下来实例演习下 二.准备工作 一.原理及其使用 生产上误删数据.误改数据的现象也是时常发生的现象,作为运维这时候 ...