48. Rotate Image

来源:互联网 发布:linux启动终端快捷键 编辑:程序博客网 时间:2024/05/06 10:55

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[][] A) {        int l = A.length;        if(l < 2) return;        for(int i = 0;i <= (l-1)/2;i++){            for(int j = i;j < l-1-i;j++){                int tmp = A[i][j];                A[i][j] = A[l-1-j][i];            //每四个位置组成了一个循环                A[l-1-j][i] = A[l-1-i][l-1-j];                A[l-1-i][l-1-j] = A[j][l-1-i];                A[j][l-1-i] = tmp;            }        }    }}
0 0
原创粉丝点击