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

[Swift]LeetCode867. 转置矩阵 | Transpose Matrix

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

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

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

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

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

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over it\'s main diagonal, switching the row and column indices of the matrix. 

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]

Example 2:

Input: [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]] 

Note:

  1. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

给定一个矩阵 A, 返回 A 的转置矩阵。

矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。 

示例 1:

输入:[[1,2,3],[4,5,6],[7,8,9]]
输出:[[1,4,7],[2,5,8],[3,6,9]]

示例 2:

输入:[[1,2,3],[4,5,6]]
输出:[[1,4],[2,5],[3,6]] 

提示:

  1. 1 <= A.length <= 1000
  2. 1 <= A[0].length <= 1000

Runtime: 92 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func transpose(_ A: [[Int]]) -> [[Int]] {
 3         var tmp = Array(repeating: (Array(repeating:0, count: A.count)), count: A[0].count)
 4         for i in 0..<A[0].count {
 5             for j in 0..<A.count {
 6                 tmp[i][j] = A[j][i]
 7             }
 8         }
 9         
10         return tmp
11     }
12 }

Runtime: 92 ms
Memory Usage: 19.2 MB
 1 class Solution {
 2     func transpose(_ A: [[Int]]) -> [[Int]] {
 3         if A.count == 0 {
 4             return []
 5         }
 6         let count = A[0].count
 7         var R: [[Int]] = []
 8         for j in 0..<count {
 9           var r: [Int] = []
10             for i in 0..<A.count {
11                 r.append(A[i][j])
12             }
13             R.append(r)
14         }
15         
16         return R        
17     }
18 }

116ms

 1 class Solution {
 2 func transpose(_ A: [[Int]]) -> [[Int]] {
 3     var result: [[Int]] = []
 4     for i in 0..<A[0].count {
 5         result.append([])
 6         for j in 0..<A.count {
 7             result[i].append(A[j][i])
 8         }
 9     }
10     return result
11   }
12 }

124ms

 1 class Solution {
 2     func transpose(_ A: [[Int]]) -> [[Int]] {
 3         var x = A.count
 4         var y = A[0].count
 5         var result: [[Int]] = []
 6         
 7         for i in 0..<y {
 8             var list: [Int] = []
 9             for j in 0..<x {
10                 list += [A[j][i]]
11             }
12             result += [list]
13         }
14         
15         return result        
16     }
17 }

128ms

class Solution {
    func transpose(_ A: [[Int]]) -> [[Int]] {
        
        var row = -1
        var column = -1
        let result = Array(count: A[0].count) { () -> [Int] in
            row += 1
            column = -1
            return Array(count: A.count) { () -> Int in
                column += 1
                return A[column][row]
            }
        } 
        return result
    }
}

public extension Array {
    public init(count: Int, generator: @escaping() -> Element) {
        precondition(count >= 0, "arrays must have non-negative size")
        self.init((0..<count).lazy.map { Element in generator() })
    }
}

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
swift - AES 加解密发布时间:2022-07-13
下一篇:
ios Swift 之github - tinkl发布时间: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