Oracle Interview Question
Software Engineer / Developers// Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place
// COMPLEXITY O (N^2)
public static void rotateM(int M[][],int N){
for( int layer = 0; layer <= N/2; layer++){
int last = N - 1 - layer;
for(int i = layer; i < last; i++){
int offset = i - layer;
// top
int temp = M[layer][i];
// top = left
M[layer][i] = M[last - offset][layer];
// left = buttom
M[last - offset][layer] = M[last][last-offset];
// bottom = right
M[last][last - offset] = M[layer + offset][last];
// right = top
M[layer + offset][last] = temp;
}
}
}
int temp;
- Varsha Vasan April 10, 2014for(int x=0;x<3;x++)
{System.out.println(" ");
for(int y=0;y<3;y++)
{temp=M[x][y];
M[x][y]=M[y][x];
M[y][x]=temp;
System.out.print(" "+M[x][y]);}}