Skip to content

Instantly share code, notes, and snippets.

@iwilbert
Created June 17, 2014 21:00
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 iwilbert/3a97e8dc2ec528a15602 to your computer and use it in GitHub Desktop.
Save iwilbert/3a97e8dc2ec528a15602 to your computer and use it in GitHub Desktop.
public void rotate(int[][] matrix) {
if(matrix == null)
return;
int N = matrix.length;
for(int i = 0; i < N/2; i++) // This is the layer
for(int j = i; j < N-i-1; j++) { // This is the offset to start
// swap
int t = matrix[i][j];
matrix[i][j] = matrix[N-j-1][i];
matrix[N-j-1][i] = matrix[N-i-1][N-j-1];
matrix[N-i-1][N-j-1] = matrix[j][N-i-1];
matrix[j][N-i-1] = t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment