• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode311.稀疏矩阵相乘$SparseMatrixMultiplication

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10706012.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode789.逃脱阻碍者|EscapeTheGhosts发布时间:2022-07-13
下一篇:
苹果发布【新开发语言】Swift发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap