161. Rotate image
Given an N×N two-dimensional matrix to represent the image, rotate the image clockwise by 90 degrees.
Sample
Example 1:
Input: [[1,2],[3,4]]
Output: [[3,1],[4,2]]
Example 2:
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6 ,3]]
challenge
Can it be done in situ?
void rotate(vector<vector<int>> &matrix) {
// write your code here
if(matrix.size() == 0 || matrix.size() == 1)
{
return;
}
int size = matrix[0].size();
vector<vector<int>> ret(size,vector<int>(size,0));
for(int row=0;row<size;row++)
{
for(int col=0;col<size;col++)
{
ret[col][size-1-row] = matrix[row][col];
}
}
matrix = ret;
}