美文网首页
Leetcode 48. Rotate Image

Leetcode 48. Rotate Image

作者: persistent100 | 来源:发表于2017-06-03 09:46 被阅读0次

题目

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?

分析

给定一个N*N的二维数组,求其顺时针旋转90度后的数组。
只要简单分析一下数字之间的对应关系即可。C代码如下已通过。

void rotate(int** matrix, int matrixRowSize, int matrixColSize) {
    int **ans=(int **)malloc(sizeof(int *)*matrixRowSize);
    for(int i=0;i<matrixRowSize;i++)
    {
        ans[i]=(int *)malloc(sizeof(int)*matrixRowSize);
        for(int j=0;j<matrixRowSize;j++)
        {
            ans[i][j]=matrix[matrixRowSize-j-1][i];
        }
    }
    for(int i=0;i<matrixRowSize;i++)
    {
        for(int j=0;j<matrixRowSize;j++)
        {
            matrix[i][j]=ans[i][j];
        }
    }
}

相关文章

网友评论

      本文标题:Leetcode 48. Rotate Image

      本文链接:https://www.haomeiwen.com/subject/qmmpfxtx.html