LeetCode - Rotate Image

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?

void rotate(vector<vector<int> > &matrix) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function

    int size = matrix.size();
    vector<vector<int> > tmp;

    for (int i = 0; i < size; ++i)
    {
        vector<int> t(size);
        for (int j = 0; j < size; ++j)
        {
            t[i] = matrix[size - 1 - j][i];
        }
        tmp.push_back(t);
    }

    matrix = tmp;
}


你可能感兴趣的:(LeetCode - Rotate Image)