Transpose Matrix
Turn each row of a matrix into a column, so a grid with m rows and n columns becomes one with n rows and m columns.
You are given a 2D integer array matrixmatrix
Example 1
Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Example 2
Output: [[1, 4], [2, 5], [3, 6]]
Explanation: a matrix with 2 rows and 3 columns becomes one with 3 rows and 2 columns.
The Idea
You have probably seen a table turned on its side. A sheet with months along the top and names down the side gets flipped, so the months run down the side and the names along the top. Spreadsheet programs call this transposing, and this problem asks for the same thing on a grid of numbers.
In Java the grid is a 2D array, also called a matrix. matrix[i]imatrix[i][j]ij
Seeing whole rows move is one thing, but code copies one value at a time. So for a single value, where exactly does it go? Look at the picture below.
On the left, row 1 of matrix
That holds for every value: the value in row ijji
Solution Approach
Turning that rule into code means settling two things: how big the result is, and in what order to fill it. A third, optional question is whether a square matrix can be transposed without a new array.
The size of the result
How many rows and columns does the result need? Call the number of rows in matrixmatrixnew int[n][m]
Giving the result the input's shape, new int[m][n]result[2][0]ArrayIndexOutOfBoundsException
Filling the result
In what order should the values be copied? Any order works, because each value has exactly one place to go and no two values share a place. The simplest is to read matrix
Transposing a square matrix in place
Could a square matrix, with as many rows as columns, skip the new array? The result has the same shape, so the values can be swapped inside matrixj > iij
Trying both ways on [[1, 2, 3], [4, 5, 6], [7, 8, 9]]j > i[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Java Solution
The method transpose
- First, read the size of the input:
is the number of rows, andm = matrix.length is the number of columns.n = matrix[0].length - Create the result with the shape flipped,
: n rows of m values.result = new int[n][m] - Visit every value of
with two loops, one inside the other. The outer loop movesmatrix over the rows, and the inner loop movesi over the columns of rowj .i - Copy each value to its swapped position with
.result[j][i] = matrix[i][j] - When both loops are done, return
.result
The main
import java.util.Arrays;
public class TransposeMatrix {
public static int[][] transpose(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] result = new int[n][m]; // the shape flips: m x n becomes n x m
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[j][i] = matrix[i][j]; // row i, column j goes to row j, column i
}
}
return result;
}
public static void main(String[] args) {
int[][] square = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(Arrays.deepToString(transpose(square)));
System.out.println(Arrays.deepToString(transpose(new int[][] {{1, 2, 3}, {4, 5, 6}})));
System.out.println(Arrays.deepToString(transpose(new int[][] {{1, 2, 3}})));
System.out.println(Arrays.deepToString(transpose(new int[][] {{7}, {8}, {9}})));
System.out.println(Arrays.deepToString(transpose(new int[][] {{5}})));
System.out.println(Arrays.deepToString(square)); // the input is not changed
}
}
Running it prints:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
[[1, 4], [2, 5], [3, 6]]
[[1], [2], [3]]
[[7, 8, 9]]
[[5]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Look at the second line, [[1, 4], [2, 5], [3, 6]]
The single row [[1, 2, 3]][[1], [2], [3]][[7], [8], [9]][[7, 8, 9]][[5]]
The last line is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]resultmatrix
The original implementation,
Complexity
The work is one copy of every value into a new array.
- Time: O(m × n), because the two loops visit each of the m × n values once, m rows of n columns, with one read and one write each.
- Space: O(m × n) for the result.
holds a copy of every value. Besides it, the method keeps onlyresult ,m ,n andi .j
Transposing a square matrix and then reversing each row turns it 90 degrees clockwise, the rotation asked for in