码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Rotate Image

时间:2014-11-14 17:15:48      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   for   div   on   

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?

 

Solution:

public class Solution {
    public void rotate(int[][] matrix) {
        int N=matrix.length-1;
        if(N<1)
            return;
        for(int i=0;i<=N/2;++i){
            for(int j=i;j<N-i;++j){
                int temp=matrix[N-j][i];
                matrix[N-j][i]=    matrix[N-i][N-j];
                matrix[N-i][N-j]=matrix[j][N-i];
                matrix[j][N-i]=matrix[i][j];
                matrix[i][j]=temp;
            }
        }
    }
}

 

[Leetcode] Rotate Image

标签:style   blog   io   color   ar   sp   for   div   on   

原文地址:http://www.cnblogs.com/Phoebe815/p/4097669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!