rotate image

来源:互联网 发布:陈星网络情缘酷我音乐 编辑:程序博客网 时间:2024/06/05 05:23

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 len = matrix.length;        for(int i = 0; i < len/2; i++)        {            for(int j = 0; j < (len + 1)/2; j++)            {                int temp                    = matrix[i][j];                matrix[i][j]                = matrix[len-1-j][i];                matrix[len-1-j][i]          = matrix[len-1-i][len-1-j];                matrix[len-1-i][len-1-j]    = matrix[j][len-1-i];                matrix[j][len-1-i]          = temp;            }        }    }}




0 0
原创粉丝点击