leetcode

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

0885.cpp (681B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> spiralMatrixIII(const int rows, const int cols, int x, int y) {
      4         static const int8_t offset_x[4] = {0, 1, 0, -1};
      5         static const int8_t offset_y[4] = {1, 0, -1, 0};
      6         vector<vector<int>> res;
      7         res.reserve(rows * cols);
      8         int dir = 0, cnt = 1, len = 1;
      9 
     10         while (res.size() < rows * cols) {
     11             for (int i = 0; i < len; i++) {
     12                 if (x >= 0 && x < rows && y >= 0 && y < cols) res.push_back({x, y});
     13                 x += offset_x[dir], y += offset_y[dir];
     14             }
     15             len += dir & 1;
     16             dir = (dir + 1) & 0x3;
     17         }
     18 
     19         return res;
     20     }
     21 };