矩阵转换,注意下标索引的正确映射。
class Solution {
func transpose(_ A: [[Int]]) -> [[Int]] {
guard A.count >= 1 && A[0].count >= 1 else {
return [[Int]]()
}
var a: [Int] = Array(repeating: 0, count: A.count)
var ret: [[Int]] = Array(repeating: a, count: A[0].count)
for j in 0..<A[0].count {
for i in 0..<A.count {
ret[j][i] = A[i][j]
}
}
return ret
}
}
网友评论