Rotate Image
2015-07-14 17:16
267 查看
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
思路:先将矩阵转置,然后第一列和最后一列交换,第二列和倒数第二列交换,第三列和倒数第三列交换….直到第n/2列和倒数第n/2列交换完成即为最终的结果。
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
思路:先将矩阵转置,然后第一列和最后一列交换,第二列和倒数第二列交换,第三列和倒数第三列交换….直到第n/2列和倒数第n/2列交换完成即为最终的结果。
public class Solution { public void rotate(int[][] matrix) { int n=matrix.length; if(n<=0) return; int m=matrix[0].length; if(m!=n) return; for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { int temp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=temp; } } for(int j=0;j<=(n-1)/2;j++) { for(int i=0;i<n;i++) { int temp=matrix[i][j]; matrix[i][j]=matrix[i][n-1-j]; matrix[i][n-1-j]=temp; } } return; } }
相关文章推荐
- 公司注册流程及税务相关知识整理
- Android&nbsp;编程规范
- Android&nbsp;NFC开发读写NDEF数据实例
- Android&nbsp;内存溢出的几点解决方法
- Android&nbsp;内存优化的几点知识
- 编写高效的android代码(译文)
- Android 获取系统相册里的照片及缩略图
- Android&nbsp;使用SmsManager发送短信
- Android&nbsp;申请签名证书详解
- Android&nbsp;复制文件的一种方法
- Android&nbsp;解析XML
- Android&nbsp;读取系统日志LogCat
- Android&nbsp;蓝牙编程
- Android&nbsp;设置全屏及自定义标题栏
- Android&nbsp;List与数组的相互转化
- Android&nbsp;监听EditText的状态改变
- Android 调用照相机拍照把照片转化为字节数组
- Android 解决Error generating final archive: Debug Certificate 问题
- Android 关于用JSONObject的new JSONObject(String s)构建含有特殊符号的字符串时报错的解决方案
- Android ScrollView与ListView一起(嵌套)使用解决ListView自动滚动的几点方案