[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 = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
                  | 0 0 1 |


给定两个稀疏矩阵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 |

Solution:
 1 class Solution {
 2     func multiply(_ A:inout [[Int]],_ B:inout [[Int]]) ->[[Int]] {
 3         var res:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:B[0].count),count:A.count)
 4         for i in 0..<A.count
 5         {
 6             for k in 0..<A[0].count
 7             {
 8                 if A[i][k] != 0
 9                 {
10                     for j in 0..<B[0].count
11                     {
12                         if B[k][j] != 0
13                         {
14                             res[i][j] += A[i][k] * B[k][j]
15                         }
16                     }
17                 }
18             }
19         }
20         return res
21     }
22 }


Solution:

 1 class Solution {
 2     func multiply(_ A:inout [[Int]],_ B:inout [[Int]]) ->[[Int]] {
 3         var res:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:B[0].count),count:A.count)
 4         var v:[[(Int,Int)]] = [[(Int,Int)]](repeating:[(Int,Int)](),count:A.count)
 5         for i in 0..<A.count
 6         {
 7             for k in 0..<A[i].count
 8             {
 9                 if A[i][k] != 0
10                 {
11                     v[i].append((k, A[i][k]))
12                 }
13             }
14         }
15         for i in 0..<A.count
16         {
17             for k in 0..<v[i].count
18             {
19                 var col:Int = v[i][k].0
20                 var val:Int = v[i][k].1
21                 for j in 0..<B[0].count
22                 {
23                     res[i][j] += val * B[col][j]
24                 }
25             }
26         }
27         return res
28     }
29 }

点击:Playground测试

1 var A:[[Int]] = [[ 1, 0, 0],[-1, 0, 3]]
2 var B:[[Int]] = [[ 7, 0, 0 ],[ 0, 0, 0 ],[ 0, 0, 1 ]]
3 let sol = Solution()
4 print(sol.multiply(&A,&B))
5 //Print [[7, 0, 0], [-7, 0, 3]]


原文地址:https://www.cnblogs.com/strengthen/p/10706012.html

时间: 2024-10-29 09:07:50

[Swift]LeetCode311. 稀疏矩阵相乘 $ Sparse Matrix Multiplication的相关文章

稀疏矩阵乘法 &#183; Sparse Matrix Multiplication

[抄题]: 给定两个 稀疏矩阵 A 和 B,返回AB的结果.您可以假设A的列数等于B的行数. [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 如果为零则不相乘,优化常数的复杂度. [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]: [复杂度]:Time complexity: O() Space comple

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

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

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

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

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

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