From 41ae96c4256a2a4039393461826cd42e143a9f61 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 3 Sep 2021 11:27:12 +0800 Subject: [PATCH] =?UTF-8?q?311:=E7=A8=80=E7=96=8F=E7=9F=A9=E9=98=B5?= =?UTF-8?q?=E7=9A=84=E4=B9=98=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/SparseMatrixMultiplication.java | 56 +++++++++++++++++++ .../doc/content/SparseMatrixMultiplication.md | 28 ++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/SparseMatrixMultiplication.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/SparseMatrixMultiplication.md diff --git a/src/main/java/leetcode/editor/cn/SparseMatrixMultiplication.java b/src/main/java/leetcode/editor/cn/SparseMatrixMultiplication.java new file mode 100644 index 0000000..a2f9077 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SparseMatrixMultiplication.java @@ -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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/SparseMatrixMultiplication.md b/src/main/java/leetcode/editor/cn/doc/content/SparseMatrixMultiplication.md new file mode 100644 index 0000000..f19da70 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/SparseMatrixMultiplication.md @@ -0,0 +1,28 @@ +

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

+ +

请仔细阅读下面的示例。

+ +

 

+ +

示例:

+ +
输入:
+
+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
  • \ No newline at end of file