From 05a136f6ce89f6e6bace473944f37e69064e754f Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 8 Aug 2024 16:33:08 +0300 Subject: [PATCH 1/2] feat: add ts solution to lc problem: No.0885 --- .../0885.Spiral Matrix III/README.md | 29 +++++++++++++++++++ .../0885.Spiral Matrix III/README_EN.md | 29 +++++++++++++++++++ .../0885.Spiral Matrix III/Solution.ts | 24 +++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 solution/0800-0899/0885.Spiral Matrix III/Solution.ts diff --git a/solution/0800-0899/0885.Spiral Matrix III/README.md b/solution/0800-0899/0885.Spiral Matrix III/README.md index db39ac38dcf8e..1a8d59a2041d0 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 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..90ddb7c8cd169 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 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; +} From 2f61b8ca9cf639edfd6a09b44a9f81bdaf64f55a Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 8 Aug 2024 16:34:53 +0300 Subject: [PATCH 2/2] feat: update js solution to lc problem: No.0885 --- .../0885.Spiral Matrix III/README.md | 40 +++++++++---------- .../0885.Spiral Matrix III/README_EN.md | 40 +++++++++---------- .../0885.Spiral Matrix III/Solution.js | 40 +++++++++---------- 3 files changed, 54 insertions(+), 66 deletions(-) diff --git a/solution/0800-0899/0885.Spiral Matrix III/README.md b/solution/0800-0899/0885.Spiral Matrix III/README.md index 1a8d59a2041d0..6e43c029af6fd 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README.md @@ -213,31 +213,27 @@ function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: num * @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 90ddb7c8cd169..694b20446f4c5 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/README_EN.md +++ b/solution/0800-0899/0885.Spiral Matrix III/README_EN.md @@ -209,31 +209,27 @@ function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: num * @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; };