Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tusharbihani/495dacf091c4a8a98a3917640d8ed431 to your computer and use it in GitHub Desktop.
Save tusharbihani/495dacf091c4a8a98a3917640d8ed431 to your computer and use it in GitHub Desktop.
Programming challenge: rotating a matrix 90 degrees in place
// Programming challenge: rotating a matrix 90 degrees in place
// Original post: https://blog.svpino.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place
public class RotatingMatrix90DegreesInPlace {
private static int[][] matrix = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }
};
private final static int N = 4;
private static void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print("\t" + matrix[i][j]);
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
print();
for (int ring = 0; ring < N / 2; ring++) {
int farthest = N - ring - 1;
for (int i = ring; i < farthest; i++) {
int temp = matrix[ring][i];
matrix[ring][i] = matrix[farthest - i + ring][ring];
matrix[farthest - i + ring][ring] =
matrix[farthest][farthest - i + ring];
matrix[farthest][farthest - i + ring] =
matrix[i][farthest];
matrix[i][farthest] = temp;
}
}
print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment