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;
    }
}
posted @ 2012-10-07 21:35  紫红的泪  阅读(2896)  评论(0编辑  收藏  举报