Skip to content

Instantly share code, notes, and snippets.

@k1xme
Created June 18, 2014 13:37
Show Gist options
  • Save k1xme/3c4ba6084f62f4f56c4d to your computer and use it in GitHub Desktop.
Save k1xme/3c4ba6084f62f4f56c4d to your computer and use it in GitHub Desktop.
class Solution{
/*
* Space: O(1); Time: O(n^2).
*/
public static int[][] rotate(int[][] image){
if(image == null) return image;
int n = image[0].length;
if(n != image.length) return image;
int layer = 0; // Rotation layer.
int temp;
for(;layer< n/2; layer++)
for(int i = layer; i< n - layer - 1; i++){ // Swap the sides of current layer element by element.
temp = image[layer][i];
image[layer][i] = image[n-layer-i-1][layer];
image[n-layer-i-1][layer] = image[n-layer-1][n-layer-i-1];
image[n-layer-1][n-layer-i-1] = image[i][n-layer-1];
image[i][n-layer-1] = temp;
}
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment