/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/matrix/MatrixTranspose.java
46 строк
1 KB
varada610
Refactor files to be in correctly nested packages (#6120)
11 янв 2025, 10:17
Не верифицирован
11 янв 2025, 10:17
1e6ed97
Код
Авторство
О чём код?
package com.thealgorithms.matrix; /** * * * <h1>Find the Transpose of Matrix!</h1> * * Simply take input from the user and print the matrix before the transpose and * after the transpose. * * <p> * <b>Note:</b> Giving proper comments in your program makes it more user * friendly and it is assumed as a high quality code. * * @author Rajat-Jain29 * @version 11.0.9 * @since 2014-03-31 */ public final class MatrixTranspose { private MatrixTranspose() { } /** * Calculate the transpose of the given matrix. * * @param matrix The matrix to be transposed * @throws IllegalArgumentException if the matrix is empty * @throws NullPointerException if the matrix is null * @return The transposed matrix */ public static int[][] transpose(int[][] matrix) { if (matrix == null || matrix.length == 0) { throw new IllegalArgumentException("Matrix is empty"); } int rows = matrix.length; int cols = matrix[0].length; int[][] transposedMatrix = new int[cols][rows]; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { transposedMatrix[i][j] = matrix[j][i]; } } return transposedMatrix; } }