[leetcode刷题系列]Rotate Image

算是模拟题把, 至于in-place,简单想下就好了, 规律还是很容易发现的。只是写的时候下标要想清楚,别写错。

class Solution {
    void change(int &a, int &b, int &c, int &d){
        int tmp = d;
        d = c;
        c = b;
        b = a;
        a = tmp;
    }
public:
    void rotate(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int n = matrix.size();
        for(int i = 0;i < n / 2; ++ i){
            for(int j = i; j + i + 1 < n; ++ j)
                change(matrix[i][j], matrix[j][n - 1 -i], matrix[n - 1 - i][n - 1 -j], matrix[n - 1 - j][i]);
        }
    }
};


你可能感兴趣的:([leetcode刷题系列]Rotate Image)