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).
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".
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]; } }
相关文章推荐
- 退出子进程的办法
- 给定一个日期,输出这个日期是该年的第几天。 C语言来做
- Mac下使用svn
- HDU 1010 Tempter of the Bone
- 转: requestAnimationFrame,Web中写动画的另一种选择
- ViewTreeObserver简介
- Apache本地可以显示外网不可以
- 别业
- 退出窗口时出现“当”的响声
- Revit 二次开发之安装包的制作
- MATLAB三维散点图的绘制(scatter3、plot3)
- 图片压缩(pc端和移动端都适用)
- UI设计
- python常用的GUI工具
- HDU_1004 balloons
- 图的割边
- <em>标签
- Android内存管理
- 项目不同阶段的风险特征
- NYOJ 1057 寻找最大数(三)