您的位置:首页 > 其它

Rotate Image

2015-07-24 21:23 127 查看
You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

public static void rotate(int[][] matrix) {
int len=matrix.length;
int[][] t=new int[len][len];
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
t[j][len-1-i]=matrix[i][j];
}
matrix=t;
}
The problem is that Java is pass by value not by refrence! "matrix" is just a reference

to a 2-dimension array. If "matrix" is assigned to a new 2-dimension array in the method,

the original array does not change. Therefore, there should be another loop to assign

each element to the array referenced by "matrix".

public static void rotate(int[][] matrix) {
int len=matrix.length;
int[][] t=new int[len][len];
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
t[j][len-1-i]=matrix[i][j];
}
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
matrix[i][j]=t[i][j];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: