Leetcode Note: Go - Transpose Matrix
Transpose Matrix - LeetCode
https://leetcode.com/problems/transpose-matrix/
- Go 言語で取り組んだメモ
所感
- 2D データとして int[][] matrix が渡されるので行と列を切り替えて return する
回答
Transpose Matrix - LeetCode
https://leetcode.com/problems/transpose-matrix/solution/
Go golang clean solution - LeetCode Discuss
https://leetcode.com/problems/transpose-matrix/discuss/351437/Go-golang-clean-solution
func transpose(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
res := make([][]int, n)
for i := range res { res[i] = make([]int, m) }
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
res[j][i] = matrix[i][j]
}
}
return res
}