Skip to content

Instantly share code, notes, and snippets.

@aaani
Last active December 26, 2015 13:19
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 aaani/7157733 to your computer and use it in GitHub Desktop.
Save aaani/7157733 to your computer and use it in GitHub Desktop.
template<int N>
void rotate90Clockwise(int (&matrix)[N][N]){
//Transpose the matrix
int temp;
for(int i=0;i<N;i++){
for(int j=0;j<i;j++){
temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
//For clockwise rotation reverse rows, for anti-clockwise reverse columns
for(int i=0;i<N;i++){
for(int j=0;j<N/2;j++){
temp = matrix[i][j];
matrix[i][j] = matrix[i][N-j-1];
matrix[i][N-j-1] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment