首页 > 其他 > 详细

48. Rotate Image

时间:2017-02-02 15:33:47      阅读:236      评论:0      收藏:0      [点我收藏+]

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?

此题可以用3*3的矩阵来举例子,发现可以先x,y互相先对掉,然后再左右对掉,代码如下:

public class Solution {

    public void rotate(int[][] matrix) {

        for(int i=0;i<matrix.length;i++){

            for(int j=0;j<matrix[0].length;j++){

                if(i>j){

                    int temp = matrix[i][j];

                    matrix[i][j] = matrix[j][i];

                    matrix[j][i] = temp;

                }

            }

        }

        for(int i=0;i<matrix[0].length/2;i++){

            for(int j=0;j<matrix.length;j++){

                int temp = matrix[j][i];

                matrix[j][i] = matrix[j][matrix[0].length-i-1];

                matrix[j][matrix[0].length-i-1]=temp;

            }

        }

    }

}

48. Rotate Image

原文:http://www.cnblogs.com/codeskiller/p/6361210.html

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