311:稀疏矩阵的乘法

This commit is contained in:
huangge1199 2021-09-03 11:27:12 +08:00
parent a2e1eed847
commit 41ae96c425
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,56 @@
//给你两个 稀疏矩阵 A B请你返回 AB 的结果你可以默认 A 的列数等于 B 的行数
//
// 请仔细阅读下面的示例
//
//
//
// 示例
//
// 输入
//
//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 |
//
// Related Topics 数组 哈希表 矩阵 👍 55 👎 0
package leetcode.editor.cn;
//311:稀疏矩阵的乘法
class SparseMatrixMultiplication {
public static void main(String[] args) {
//测试代码
Solution solution = new SparseMatrixMultiplication().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[][] multiply(int[][] mat1, int[][] mat2) {
int[][] result = new int[mat1.length][mat2[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
for (int k = 0; k < mat1[0].length; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>给你两个&nbsp;<a href="https://baike.baidu.com/item/%E7%A8%80%E7%96%8F%E7%9F%A9%E9%98%B5" target="_blank">稀疏矩阵</a>&nbsp;<strong>A</strong>&nbsp;&nbsp;<strong>B</strong>,请你返回&nbsp;<strong>AB</strong> 的结果。你可以默认&nbsp;<strong>A&nbsp;</strong>的列数等于&nbsp;<strong>B&nbsp;</strong>的行数。</p>
<p>请仔细阅读下面的示例。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:
A</strong> = [
[ 1, 0, 0],
[-1, 0, 3]
]
<strong>B</strong> = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
<strong>输出:</strong>
| 1 0 0 | | 7 0 0 | | 7 0 0 |
<strong>AB</strong> = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
</pre>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>矩阵</li></div></div><br><div><li>👍 55</li><li>👎 0</li></div>