diff --git a/solution/0800-0899/0885.Spiral Matrix III/README.md b/solution/0800-0899/0885.Spiral Matrix III/README.md index db39ac38dcf8e..6e43c029af6fd 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README.md @@ -173,6 +173,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### TypeScript + +```ts +function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] { + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans: number[][] = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); + } + } + + i = (i + 1) % 4; + } + + return ans; +} +``` + #### JavaScript ```js @@ -184,31 +213,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { * @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]); + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); } } - d = (d + 1) % 4; + + i = (i + 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 0408075aab7df..694b20446f4c5 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,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { } ``` +#### TypeScript + +```ts +function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] { + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans: number[][] = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); + } + } + + i = (i + 1) % 4; + } + + return ans; +} +``` + #### JavaScript ```js @@ -180,31 +209,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int { * @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]); + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); } } - d = (d + 1) % 4; + + i = (i + 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 index 01f66eeb78e6c..2985f1f5fdc2b 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/Solution.js +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.js @@ -6,30 +6,26 @@ * @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]); + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); } } - d = (d + 1) % 4; + + i = (i + 1) % 4; } + return ans; }; diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.ts b/solution/0800-0899/0885.Spiral Matrix III/Solution.ts new file mode 100644 index 0000000000000..5fe6864760b4f --- /dev/null +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.ts @@ -0,0 +1,24 @@ +function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] { + // prettier-ignore + const dir = [[1,0],[0,1],[-1,0],[0,-1]] + let [x, y, i, size] = [cStart, rStart, 0, 0]; + const ans: number[][] = [[y, x]]; + const total = rows * cols; + + while (ans.length < total) { + if (i % 2 === 0) size++; + + for (let j = 0; ans.length < total && j < size; j++) { + x += dir[i][0]; + y += dir[i][1]; + + if (0 <= x && x < cols && 0 <= y && y < rows) { + ans.push([y, x]); + } + } + + i = (i + 1) % 4; + } + + return ans; +}