09 Sept 2026Go / Python / TypeScriptEasy

Transpose Matrix

Return the matrix formed by swapping every input row and column.

Allocate a columns-by-rows result and copy each input cell to the coordinates with reversed indices.

complexity

O(rows * columns) time and O(rows * columns) output space.

solution files

  • Go transpose-matrix/solution.go
  • Python transpose-matrix/solution.py
  • TypeScript transpose-matrix/solution.ts

Solution files

Gotranspose-matrix/solution.go
package main

func transpose(matrix [][]int) [][]int {
	result := make([][]int, len(matrix[0]))
	for column := range result {
		result[column] = make([]int, len(matrix))
		for row := range matrix {
			result[column][row] = matrix[row][column]
		}
	}
	return result
}
Pythontranspose-matrix/solution.py
class Solution:
    def transpose(self, matrix: list[list[int]]) -> list[list[int]]:
        return [list(column) for column in zip(*matrix)]
TypeScripttranspose-matrix/solution.ts
function transpose(matrix: number[][]): number[][] {
  return Array.from({ length: matrix[class="syntax-number">0].length }, (_, column) => matrix.map((row) => row[column]));
}