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 ...
随机推荐
- DolphinPHP(海豚框架)初步学习
由于工作原因需要学习DolphinPHP框架,在此记录一下! 首先交代下环境,我用的是phpStudy集成环境,然后海豚框架是最新的1.4.2版本 ok,接下来我们开始学习,最基础的,是我们需要将我们 ...
- CSS中position和float的使用
近期会更新一系列博客,对基础知识再度做个巩固和梳理. 一.position定位 (一):position的属性 1.absolute:生成绝对定位的元素,相对于最近一级定位不是static的父元素来进 ...
- Linux iptables常用防火墙规则
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机) iptables -A INPUT -m stat ...
- 【IDEA使用技巧】(1) —— 快捷键
1.InteliJ IDEA设置快捷键 1.1. IDEA快捷键修改—代码提示 IDEA中当现有的快捷键被系统中其他软件(比如输入法)占用时,我们可以自定义修改快捷键.比如,IDEA中的代码自动提示快 ...
- centos6.5升级openssh至7.9p1
环境说明系统环境:centos 6.5 x64 openssh-5.3p1升级原因:低版本openssh存在漏洞升级目标:openssh-7.9p1 检查环境官方文档中提到的先决条件openssh安装 ...
- Winform 使用热键功能实现Ctrl+C和Ctrl+V复制粘贴功能
当我们使用winform控件的时候,会发现这些控件(比如Label)不支持Ctrl+c 复制和Ctrl+v 快捷键复制粘贴功能,如果我们需要实现这个功能改怎么做呢? 1. 首先我们创建一个winfor ...
- ASP.NET MVC+Entity Framework code first 迁移
再来一张,选择 MVC 模版,其他的没选过,不会用 =_=!! 身份验证用个人用户账户,这个是为了偷懒,话说 ASP.NET Identity 还是很给力的,不用白不用 ^_^~ 点击确定之后,会看 ...
- Ubuntu 18.04 LTS版本 谷歌拼音输入法安装
为何安装? 自带IBUS框架对中文支持不稳定 采用对中文支持稳定的fcitx框架 如何安装? 步骤如下: 卸载自带IBUS框架 命令:sudo remove ibus 安装fcitx框架 ...
- UML回顾暨课程总结
本文作为OO的最后一次博客作业,主要回顾了第四单元的架构设计和本学期的心路历程. 本单元架构设计 UML1 第一次作业的主要内容是解析mdj格式输入,记录特定数据并支持针对类.属性和方法等的查询功 ...
- 阿里云ssl协议发布qq邮件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...