Skip to content

Commit 05a136f

Browse files
committed
feat: add ts solution to lc problem: No.0885
1 parent 2d7c1e4 commit 05a136f

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/0800-0899/0885.Spiral Matrix III/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
173173
}
174174
```
175175

176+
#### TypeScript
177+
178+
```ts
179+
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
180+
// prettier-ignore
181+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
182+
let [x, y, i, size] = [cStart, rStart, 0, 0];
183+
const ans: number[][] = [[y, x]];
184+
const total = rows * cols;
185+
186+
while (ans.length < total) {
187+
if (i % 2 === 0) size++;
188+
189+
for (let j = 0; ans.length < total && j < size; j++) {
190+
x += dir[i][0];
191+
y += dir[i][1];
192+
193+
if (0 <= x && x < cols && 0 <= y && y < rows) {
194+
ans.push([y, x]);
195+
}
196+
}
197+
198+
i = (i + 1) % 4;
199+
}
200+
201+
return ans;
202+
}
203+
```
204+
176205
#### JavaScript
177206

178207
```js

solution/0800-0899/0885.Spiral Matrix III/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,35 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
169169
}
170170
```
171171

172+
#### TypeScript
173+
174+
```ts
175+
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
176+
// prettier-ignore
177+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
178+
let [x, y, i, size] = [cStart, rStart, 0, 0];
179+
const ans: number[][] = [[y, x]];
180+
const total = rows * cols;
181+
182+
while (ans.length < total) {
183+
if (i % 2 === 0) size++;
184+
185+
for (let j = 0; ans.length < total && j < size; j++) {
186+
x += dir[i][0];
187+
y += dir[i][1];
188+
189+
if (0 <= x && x < cols && 0 <= y && y < rows) {
190+
ans.push([y, x]);
191+
}
192+
}
193+
194+
i = (i + 1) % 4;
195+
}
196+
197+
return ans;
198+
}
199+
```
200+
172201
#### JavaScript
173202

174203
```js
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: number): number[][] {
2+
// prettier-ignore
3+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
4+
let [x, y, i, size] = [cStart, rStart, 0, 0];
5+
const ans: number[][] = [[y, x]];
6+
const total = rows * cols;
7+
8+
while (ans.length < total) {
9+
if (i % 2 === 0) size++;
10+
11+
for (let j = 0; ans.length < total && j < size; j++) {
12+
x += dir[i][0];
13+
y += dir[i][1];
14+
15+
if (0 <= x && x < cols && 0 <= y && y < rows) {
16+
ans.push([y, x]);
17+
}
18+
}
19+
20+
i = (i + 1) % 4;
21+
}
22+
23+
return ans;
24+
}

0 commit comments

Comments
 (0)