diff --git a/solution/0800-0899/0885.Spiral Matrix III/README.md b/solution/0800-0899/0885.Spiral Matrix III/README.md index 746fc2d2703ba..db39ac38dcf8e 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README.md @@ -173,6 +173,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### JavaScript + +```js +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function (rows, cols, rStart, cStart) { + const ans = []; + const totalCells = rows * cols; + const directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + let step = 0; + let d = 0; + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + if (r >= 0 && r < rows && c >= 0 && c < cols) { + ans.push([r, c]); + } + } + d = (d + 1) % 4; + } + return ans; +}; +``` + diff --git a/solution/0800-0899/0885.Spiral Matrix III/README_EN.md b/solution/0800-0899/0885.Spiral Matrix III/README_EN.md index 42e57339a9b0e..0408075aab7df 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README_EN.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README_EN.md @@ -169,6 +169,46 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### JavaScript + +```js +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function (rows, cols, rStart, cStart) { + const ans = []; + const totalCells = rows * cols; + const directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + let step = 0; + let d = 0; + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + if (r >= 0 && r < rows && c >= 0 && c < cols) { + ans.push([r, c]); + } + } + d = (d + 1) % 4; + } + return ans; +}; +``` + diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.js b/solution/0800-0899/0885.Spiral Matrix III/Solution.js new file mode 100644 index 0000000000000..01f66eeb78e6c --- /dev/null +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.js @@ -0,0 +1,35 @@ +/** + * @param {number} rows + * @param {number} cols + * @param {number} rStart + * @param {number} cStart + * @return {number[][]} + */ +var spiralMatrixIII = function (rows, cols, rStart, cStart) { + const ans = []; + const totalCells = rows * cols; + const directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0], + ]; + let step = 0; + let d = 0; + let [r, c] = [rStart, cStart]; + ans.push([r, c]); + while (ans.length < totalCells) { + if (d === 0 || d === 2) { + step++; + } + for (let i = 0; i < step; i++) { + r += directions[d][0]; + c += directions[d][1]; + if (r >= 0 && r < rows && c >= 0 && c < cols) { + ans.push([r, c]); + } + } + d = (d + 1) % 4; + } + return ans; +};