LeetCode Rotate Image

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) {
    	int N = matrix.size();
		for(int layer = 0; layer < N/2; ++layer)
		{
			for(int i = layer; i < N-layer-1; ++i)
			{
				int temp = matrix[layer][i];
				matrix[layer][i] = matrix[N-1-i][layer];
				matrix[N-1-i][layer] = matrix[N-1-layer][N-1-i];
				matrix[N-1-layer][N-1-i] = matrix[i][N-1-layer];
				matrix[i][N-1-layer] = temp;
			}
		}
    }
};



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