leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1914.cpp (905B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> rotateGrid(vector<vector<int>> &grid, int k) const {
      4         const int n = size(grid), m = size(grid[0]);
      5 
      6         for (int l = 0; l < n / 2 && l < m / 2; l++) {
      7             const int elems = (m + n - 4 * l - 2) * 2;
      8             for (int count = 0; count < k % elems; count++) {
      9                 const int tmp = grid[l][l];
     10                 for (int j = l + 1; j < m - l; j++)
     11                     grid[l][j - 1] = grid[l][j];
     12                 for (int i = l + 1; i < n - l; i++)
     13                     grid[i - 1][m - l - 1] = grid[i][m - l - 1];
     14                 for (int j = m - l - 2; j >= l; j--)
     15                     grid[n - l - 1][j + 1] = grid[n - l - 1][j];
     16                 for (int i = n - l - 2; i >= l; i--)
     17                     grid[i + 1][l] = grid[i][l];
     18                 grid[l + 1][l] = tmp;
     19             }
     20         }
     21 
     22         return grid;
     23     }
     24 };