首页 > 其他 > 详细

Rotate Image

时间:2015-03-07 19:58:09      阅读:257      评论:0      收藏:0      [点我收藏+]

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;
    }
}
View Code

学习之处:

  swap方法的扩展,按照这样的替换方法 tmp = A ; A = B ; B = C; C = D; D=tmp;

Rotate Image

原文:http://www.cnblogs.com/sunshisonghit/p/4320803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!