Transpose a Matrix

Given a nonempty rectangular integer matrix, return its transpose. The value at source row r and column c must appear at result row c and column r.

Given a nonempty rectangular integer matrix, return its transpose. The value at source row r and column c must appear at result row c and column r.

1 to 1000 rows, 1 to 500 columns, at most 250,000 cells; every row has the same positive length. Every cell is an integer from -1000 through 1000. The visualizer uses matrices of at most 6 rows and 6 columns; larger valid inputs are execution-only.

Example 1: input matrix = [[1, 2, 3], [4, 5, 6]]; output [[1, 4], [2, 5], [3, 6]]. The source has two rows and three columns, so the result has three rows and two columns. Each source coordinate (r, c) maps to result coordinate (c, r).

Example 2: input matrix = [[-2], [7]]; output [[-2, 7]]. A one-column matrix becomes one result row containing the source values from top to bottom.

Example 3: input matrix = [[9]]; output [[9]]. Transposing a single-cell matrix keeps its one value at coordinate (0, 0).

Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: math.