天天看點

[LeedCode OJ]#48 Rotate Image

【 聲明:版權所有,轉載請标明出處,請勿用于商業用途。  聯系信箱:[email protected]】

題目連結:https://leetcode.com/problems/rotate-image/

題意: 給定一個二維數組,要求把這個二維數組順時針旋轉90度

思路: 這種題目我們隻需要再紙上畫一畫就能看出規律

class Solution
{
public:
    void rotate(vector<vector<int> >& a)
    {
        int n = a.size();
        if(n==0)
            return ;
        int m = a[0].size();
        vector<vector<int> > b;
        b.resize(m);
        int i,j;
        for(j = 0; j<m; j++)
        {
            b[j].resize(n);
            for(i = 0; i<n; i++)
            {
                b[j][i] = a[n-1-i][j];
            }
        }
        swap(a,b);
    }
};