题解 | #顺时针旋转矩阵#

顺时针旋转矩阵

http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc

using System;

class Solution
{
    public int[][] rotateMatrix(int[][] mat, int n)
    {
        // write code here
        //先斜对角堆成
        for(int i=0;i<n;++i){
            for(int j=i;j<n;j++)
            {
                var temp=mat[i][j];
                mat[i][j]=mat[j][i];
                mat[j][i]=temp;
            }
        }
        for(int i=0;i<n;++i)
        {
            Array.Reverse(mat[i]);
        }
        return mat;
    }
}

比起开辟一个空间,这种办法更好,特别是在矩阵特别大的时候。先斜对角变换,再做左右对称变幻
全部评论

相关推荐

点赞 评论 收藏
转发
投递美团等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务