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 |

分析:

  第1种,求解一般矩阵乘积的方法;第2种,根据稀疏矩阵的特性减少0*x的计算次数。

代码1:

public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
int m = A.size(), n = B.size(), p = B[].size();
vector<vector<int> >C(m, vector<int>(p, ));
for(int i = ; i < m; i++) {
for(int j = ; j < n; j++) {
if(A[i][j]) {
for(int k = ; k < p; k++) {
C[i][k] += A[i][j] * B[j][k];
}
}
}
}
return C;
}
};

代码2:

待补充

[Locked] Sparse Matrix Multiplication的更多相关文章

  1. 311. Sparse Matrix Multiplication

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

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

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

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

  4. Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

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

  8. LeetCode 311. Sparse Matrix Multiplication

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

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

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

随机推荐

  1. mysql - 编码

    show variables like 'character%'; 通过以上命令可以查询编码情况, 不过,在安装的时候,建议选择‘gbk’这类中文编码, 如果选择的是utf8,则在处理的过程中需要进行 ...

  2. Ubuntu Server 14.04 下root无法ssh登陆

    今天安装了Ubuntu Server 14.04   在终端配置了root密码后,使用SecureCRT和putty竟然不能ssh登陆,SecureCRT一直提示密码不对,但是可以肯定输入的密码100 ...

  3. 每天一条linux命令——login

    login命令用于给出登录界面,可用于重新登录或者切换用户身份,也可通过它的功能随时更换登入身份.当/etc/nologin文件存在时,系统只root帐号登入系统,其他用户一律不准登入. 语法: lo ...

  4. ZOJ 3872 Beauty of Array

    /** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...

  5. 自定义复选框 checkbox 样式

    默认的复选框样式一般在项目中都很少用 ,看起来也丑丑的.这里提供一个优化样式后的复选框.原理就是隐藏掉默认样式,在用设计好的样式替代 html结构 <div> <input type ...

  6. QQ空间API接口

    (以下内容可能会随着时间改变而改变!) 查看对方QQ空间的背景音乐 http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?json=0& ...

  7. BAE 环境下配置 struts2 + spring + hibernate(SSH)(一)准备

    1.首先选择版本控制 SVN 或者 Git ,但是由于Git在windows下需要环境,所以优先选择SVN. 2.安装一个SVN客户端 windows下使用TortoiseSVN:立即下载 注意:BA ...

  8. Explain语法

    EXPLAIN SELECT -- 变体: 1. EXPLAIN EXTENDED SELECT -- 将执行计划"反编译"成SELECT语句,运行SHOW WARNINGS 可得 ...

  9. pycharm去掉拼写检查

    http://zhidao.baidu.com/question/523436629.html

  10. 简述MVC思想 与PHP如何实现MVC

    我相信已经有很多这样的文章了,但是我今天还是愿意把自己的经验与大家分享一下.纯属原创,我也没什么保留,希望对新手有帮助,有说的不对的地方,也欢迎指出. 什么是MVC? 简单的说就是将网站源码分类.分层 ...