Created
October 25, 2017 16:56
-
-
Save jamesanto/d59e0e9329714c31c0131a7c2bfaad8a to your computer and use it in GitHub Desktop.
Matrix rotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MatrixUtils { | |
public static <T> boolean isSquareMatrix(T[][] matrix) { | |
if (matrix.length == 0) return true; | |
for (int i = 0; i < matrix.length; i++) { | |
if (matrix[i].length != matrix.length) return false; | |
} | |
return true; | |
} | |
public static <T> void rotateClockwise90(T[][] matrix) throws NotASquareMatrixException { | |
if (matrix.length == 0) return; | |
else if (!isSquareMatrix(matrix)) throw new NotASquareMatrixException(); | |
int n = matrix.length; | |
for (int i = 0; i < n / 2; i++) { | |
for (int j = i; j < n - i - 1; j++) { | |
T temp = matrix[i][j]; | |
matrix[i][j] = matrix[n - 1 - j][i]; | |
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]; | |
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]; | |
matrix[j][n - 1 - i] = temp; | |
} | |
} | |
} | |
public static void test3by3() throws NotASquareMatrixException { | |
Integer[][] matrix = new Integer[][]{ | |
new Integer[]{1, 2, 3}, | |
new Integer[]{4, 5, 6}, | |
new Integer[]{7, 8, 9} | |
}; | |
rotateClockwise90(matrix); | |
} | |
public static void test4by4() throws NotASquareMatrixException { | |
Integer[][] matrix = new Integer[][]{ | |
new Integer[]{1, 2, 3, 4}, | |
new Integer[]{5, 6, 7, 8}, | |
new Integer[]{9, 10, 11, 12}, | |
new Integer[]{13, 14, 15, 16} | |
}; | |
rotateClockwise90(matrix); | |
} | |
public static void main(String[] args) throws NotASquareMatrixException { | |
test3by3(); | |
test4by4(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertTrue; | |
public class MatrixUtilsTest { | |
private <T> boolean equals(T[][] m1, T[][] m2) { | |
if (m1.length == 0 && m2.length == 0) return true; | |
if (m1.length != m2.length) return false; | |
for (int i = 0; i < m1.length; i++) { | |
if (m1[i].length != m2[i].length) return false; | |
for (int j = 0; j < m1[0].length; j++) { | |
if (!m1[i][j].equals(m2[i][j])) return false; | |
} | |
} | |
return true; | |
} | |
@Test | |
public void test3By3Matrix() throws NotASquareMatrixException { | |
Integer[][] matrix = new Integer[][]{ | |
new Integer[]{1, 2, 3}, | |
new Integer[]{4, 5, 6}, | |
new Integer[]{7, 8, 9} | |
}; | |
Integer[][] expected = new Integer[][]{ | |
new Integer[]{7, 4, 1}, | |
new Integer[]{8, 5, 2}, | |
new Integer[]{9, 6, 3} | |
}; | |
MatrixUtils.rotateClockwise90(matrix); | |
assertTrue(equals(expected, matrix)); | |
} | |
@Test | |
public void test4By4Matrix() throws NotASquareMatrixException { | |
Integer[][] matrix = new Integer[][]{ | |
new Integer[]{5, 1, 9, 11}, | |
new Integer[]{2, 4, 8, 10}, | |
new Integer[]{13, 3, 6, 7}, | |
new Integer[]{15, 14, 12, 16} | |
}; | |
Integer[][] expected = new Integer[][]{ | |
new Integer[]{15, 13, 2, 5}, | |
new Integer[]{14, 3, 4, 1}, | |
new Integer[]{12, 6, 8, 9}, | |
new Integer[]{16, 7, 10, 11} | |
}; | |
MatrixUtils.rotateClockwise90(matrix); | |
assertTrue(equals(expected, matrix)); | |
} | |
@Test(expected = NotASquareMatrixException.class) | |
public void testNotASquareMatrixException() throws NotASquareMatrixException { | |
Integer[][] matrix = new Integer[][]{ | |
new Integer[]{1, 2, 3}, | |
new Integer[]{4, 5, 6, 10}, | |
new Integer[]{7, 8, 9} | |
}; | |
MatrixUtils.rotateClockwise90(matrix); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NotASquareMatrixException extends Exception { | |
public NotASquareMatrixException() { | |
this("Given matrix is not a square matrix"); | |
} | |
public NotASquareMatrixException(String message) { | |
super(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment