Given a 2D integer array matrix
, return the transpose of matrix
.
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
-109 <= matrix[i][j] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to transpose a matrix directly creates a new matrix. We fill the new matrix by taking each element from the original matrix and placing it in its transposed position in the new matrix.
Here's how the algorithm would work step-by-step:
def transpose_matrix_brute_force(matrix):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
# Create the transposed matrix with dimensions swapped.
transposed_matrix = [([0] * number_of_rows) for _ in range(number_of_columns)]
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
# Place each element in its transposed position.
transposed_matrix[column_index][row_index] = matrix[row_index][column_index]
return transposed_matrix
The goal is to swap the rows and columns of the matrix. The core idea is to create a new matrix where the element at row 'i' and column 'j' in the original matrix becomes the element at row 'j' and column 'i' in the new matrix.
Here's how the algorithm would work step-by-step:
def transpose_matrix(matrix):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
# Create the transposed matrix with swapped dimensions.
transposed_matrix = [([0] * number_of_rows) for _ in range(number_of_columns)]
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
# Populate transposed matrix by swapping row and column indices.
transposed_matrix[column_index][row_index] = matrix[row_index][column_index]
return transposed_matrix
Case | How to Handle |
---|---|
Null or undefined matrix input | Return an empty list or throw an IllegalArgumentException to indicate invalid input. |
Empty matrix (matrix with zero rows) | Return an empty list as the transpose of an empty matrix is also an empty matrix. |
Matrix with zero columns (all rows are empty) | Return an empty list as the transpose of a matrix with zero columns is also an empty matrix. |
1x1 matrix (single element) | Return a new 1x1 matrix containing the same single element. |
Non-rectangular matrix (rows have different lengths) | Throw an IllegalArgumentException or return null since a non-rectangular matrix cannot be transposed to a valid rectangular matrix. |
Matrix with large dimensions (very large number of rows or columns) | Ensure the solution doesn't consume excessive memory by allocating the transposed matrix efficiently. |
Matrix contains negative numbers, zeros, or large positive numbers | The basic transpose algorithm will work correctly with these value types without special handling. |
Integer overflow during allocation when rows * cols exceeds maximum integer value | The number of rows multiplied by the number of columns can exceed the maximum integer value, so handle that by limiting the maximum matrix size. |