题意是旋转正方形矩形。右旋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;
    }
}

相关文章:

  • 2021-07-04
  • 2021-12-04
  • 2021-09-25
  • 2022-02-03
  • 2022-02-20
猜你喜欢
  • 2021-04-18
相关资源
相似解决方案