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) {
        // Note: The Solution object is instantiated only once.
		int n = matrix.size();
        if(n<2)return;
		int loop=0;
		while(loop<(n+1)/2)
		{
			int tmp = 0;
			for(int i = loop; i< n-1-loop;i++)
			{
				tmp = matrix[loop][i];
				matrix[loop][i] = matrix[n-1-i][loop];
				matrix[n-1-i][loop] = matrix[n-1-loop][n-1-i];
				matrix[n-1-loop][n-1-i] = matrix[i][n-1-loop];
				matrix[i][n-1-loop] = tmp;
			}
			loop++;
		}
    }





你可能感兴趣的:(LeetCode,image,rotate)