Rotate Image
Rotate Image
问题:
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?
思路:
画画图就知道该怎么样替换了
我的代码:
public class Solution { public void rotate(int[][] matrix) { if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return; int n = matrix.length; for(int i = 0; i < n/2; i++) { for(int j = i; j < n - i - 1; j++) { int first = matrix[i][j]; matrix[i][j] = matrix[n-j-1][i]; matrix[n-j-1][i] = matrix[n-i-1][n-j-1]; matrix[n-i-1][n-j-1] = matrix[j][n-i-1]; matrix[j][n-i-1] = first; } } return; } }
学习之处:
swap方法的扩展,按照这样的替换方法 tmp = A ; A = B ; B = C; C = D; D=tmp;
posted on 2015-03-07 19:12 zhouzhou0615 阅读(116) 评论(0) 编辑 收藏 举报