Rotate Image,N*N矩阵顺时针旋转90度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class RotateImage {
    public void rotate(int[][] matrix)
    {
        if(matrix.length == 1 && matrix[0].length == 1)
        {
            return;
        }
        int n = matrix.length;
        for(int i = 0; i < n-1; i ++)
        {
            for(int j = i; j < n-1-i; j ++)
            {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n-1-j][i];
                matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
                matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
                matrix[j][n-1-i] = temp;
            }
        }
    }
     
    public static void main(String[] args)
    {
        RotateImage ri = new RotateImage();
        int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
        ri.rotate(a);
        //ri.rotate(a);
        //ri.rotate(a);
        //ri.rotate(a);
        for (int[] is : a)
        {
            for (int i : is)
            {
                System.out.print(i+" ");
            }
            System.out.println("\n\r");
        }
    }
}

  

posted @   32ddd  阅读(259)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示