[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?

class Solution {

public:

    void rotate(vector<vector<int> > &matrix) {

        // IMPORTANT: Please reset any member data you declared, as

        // the same Solution instance will be reused for each test case.

        if(matrix.size() == 0) return;

        vector<vector<int>> ans;

        for(int j = 0;j < matrix[0].size();j++)

        {

            vector<int> row;

            for(int i = matrix.size() - 1;i >= 0;i--)

                row.push_back(matrix[i][j]);

            ans.push_back(row);

        }

        matrix = ans;

    }

};

你可能感兴趣的:(LeetCode)