From 203d2303641b06174a0917d2d40e9a8b29554d11 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 8 Aug 2024 23:36:18 +0530 Subject: [PATCH] Create 885. Spiral Matrix III --- 885. Spiral Matrix III | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 885. Spiral Matrix III diff --git a/885. Spiral Matrix III b/885. Spiral Matrix III new file mode 100644 index 0000000..247b2a1 --- /dev/null +++ b/885. Spiral Matrix III @@ -0,0 +1,43 @@ +class Solution { +public: + vector> spiralMatrixIII(int rows, int cols, int rStart, + int cStart) { + vector> res; + int i = rStart, j = cStart; + char dir = 'R'; + int steps = 0; + + res.push_back({rStart, cStart}); + + while (res.size() != rows * cols) { + if (dir == 'R' || dir == 'L') { + steps++; + } + int curSteps = steps; + while (curSteps--) { + if (dir == 'R') { + j = j + 1; + } else if (dir == 'L') { + j = j - 1; + } else if (dir == 'U') { + i = i - 1; + } else if (dir == 'D') { + i = i + 1; + } + if (i >= 0 && i < rows && j >= 0 && j < cols) { + res.push_back({i, j}); + } + } + if (dir == 'R') { + dir = 'D'; + } else if (dir == 'L') { + dir = 'U'; + } else if (dir == 'U') { + dir = 'R'; + } else if (dir == 'D') { + dir = 'L'; + } + } + return res; + } +};