leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0885.cpp (681B)
0 class Solution { 1 public: 2 vector<vector<int>> spiralMatrixIII(const int rows, const int cols, int x, int y) { 3 static const int8_t offset_x[4] = {0, 1, 0, -1}; 4 static const int8_t offset_y[4] = {1, 0, -1, 0}; 5 vector<vector<int>> res; 6 res.reserve(rows * cols); 7 int dir = 0, cnt = 1, len = 1; 8 9 while (res.size() < rows * cols) { 10 for (int i = 0; i < len; i++) { 11 if (x >= 0 && x < rows && y >= 0 && y < cols) res.push_back({x, y}); 12 x += offset_x[dir], y += offset_y[dir]; 13 } 14 len += dir & 1; 15 dir = (dir + 1) & 0x3; 16 } 17 18 return res; 19 } 20 };