Created
June 18, 2014 13:37
-
-
Save k1xme/3c4ba6084f62f4f56c4d to your computer and use it in GitHub Desktop.
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
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