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 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 public class Solution {
 2     public int[,] Multiply(int[,] A, int[,] B) {
 3         int rowsA = A.GetLength(0), colsA = A.GetLength(1), colsB = B.GetLength(1);
 4
 5         var output = new int[rowsA, colsB];
 6         var nonZeroRows = new List<int>();
 7
 8         for (int i = 0; i < rowsA; i++)
 9         {
10             for (int j = 0; j < colsA; j++)
11             {
12                 if (A[i, j] != 0)
13                 {
14                     nonZeroRows.Add(i);
15                     break;
16                 }
17             }
18         }
19
20         foreach (var i in nonZeroRows)
21         {
22             for (int j = 0; j < colsB; j++)
23             {
24                 for (int k = 0; k < colsA; k++)
25                 {
26                     output[i, j] += A[i, k] * B[k, j];
27                 }
28             }
29         }
30
31         return output;
32     }
33 }

原文地址:https://www.cnblogs.com/liangmou/p/8369293.html

时间: 2024-10-09 17:26:23

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 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 = | -

311. Sparse Matrix Multiplication

/* * 311. Sparse Matrix Multiplication * 2016-7-4 by Mingyang * 这个题目真的是脑壳进屎了才会用HashMap做..... */ public int[][] multiply(int[][] A, int[][] B) { int m = A.length, n = A[0].length, nB = B[0].length; int[][] C = new int[m][nB]; for(int i = 0; i < m; i++

facebook 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 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 = | -

311. Sparse Matrix Multiplication - Medium

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

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 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 = | -

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

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: 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 = | -

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

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