【LeetCode】48. Rotate Image

来源:互联网 发布:蛋总的小黑淘宝可信吗 编辑:程序博客网 时间:2024/06/03 08:50

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) {        int ilen = matrix[0].length;        int temp[][] = new int[ilen][ilen];                for (int i = 0;i < ilen;i++)        {            System.arraycopy(matrix[i],0,temp[i],0,ilen);        }                ilen--;                for (int i = 0;i <= ilen;i++)        {            for (int j = 0;j <= ilen;j++)            {                matrix[i][j] = temp[ilen - j][i];            }        }    }}


原创粉丝点击