Rotate Image
题意是旋转正方形矩形。右旋90度,要求O(1)空间复杂度。
// Rotate a round which top & left is topleft, bottom & right is bottomright.
void rotate(vector<vector<int> > &matrix, size_t topleft, size_t bottomright)
{
for (size_t i = 0; i < bottomright - topleft; ++i)
{
int tmp = matrix[topleft][topleft + i];
matrix[topleft][topleft + i] = matrix[bottomright - i][topleft];
matrix[bottomright - i][topleft] = matrix[bottomright][bottomright - i];
matrix[bottomright][bottomright - i] = matrix[topleft + i][bottomright];
matrix[topleft + i][bottomright] = tmp;
}
}
void rotate(vector<vector<int> > &matrix)
{
size_t topleft = 0, bottomright = matrix.size() - 1;
while (topleft < bottomright)
{
rotate(matrix, topleft, bottomright);
++topleft; --bottomright;
}
}