您的位置:首页 > 其它

【7】Rotate matrix by 90 degrees

2013-04-13 15:18 423 查看
Question: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?

package CareerCup;

public class RotateMatrix
{
public RotateMatrix(){}

public int[][] rotate(int matrix[][],int max)
{
int[][] result = new int[max][max];
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
result[i][j] = matrix[max-1-j][i];
}
return result;
}

public void print(int matrix[][],int max)
{
for(int i=0;i<max;i++)
{
for(int j=0;j<max;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args)
{
int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int max = 4;
RotateMatrix rm = new RotateMatrix();
int[][] result = rm.rotate(matrix,max);
System.out.println("The orignal matrix:");
rm.print(matrix, max);
System.out.println("The rotated matrix:");
rm.print(result, max);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: