Skip to content

Commit e297bb8

Browse files
authored
feat: update js and add ts solution to lc problem: No.0885 (doocs#3382)
1 parent 06b41f9 commit e297bb8

File tree

4 files changed

+136
-66
lines changed

4 files changed

+136
-66
lines changed

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

Lines changed: 47 additions & 22 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
@@ -184,31 +213,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
184213
* @return {number[][]}
185214
*/
186215
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
187-
const ans = [];
188-
const totalCells = rows * cols;
189-
const directions = [
190-
[0, 1],
191-
[1, 0],
192-
[0, -1],
193-
[-1, 0],
194-
];
195-
let step = 0;
196-
let d = 0;
197-
let [r, c] = [rStart, cStart];
198-
ans.push([r, c]);
199-
while (ans.length < totalCells) {
200-
if (d === 0 || d === 2) {
201-
step++;
202-
}
203-
for (let i = 0; i < step; i++) {
204-
r += directions[d][0];
205-
c += directions[d][1];
206-
if (r >= 0 && r < rows && c >= 0 && c < cols) {
207-
ans.push([r, c]);
216+
// prettier-ignore
217+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
218+
let [x, y, i, size] = [cStart, rStart, 0, 0];
219+
const ans = [[y, x]];
220+
const total = rows * cols;
221+
222+
while (ans.length < total) {
223+
if (i % 2 === 0) size++;
224+
225+
for (let j = 0; ans.length < total && j < size; j++) {
226+
x += dir[i][0];
227+
y += dir[i][1];
228+
229+
if (0 <= x && x < cols && 0 <= y && y < rows) {
230+
ans.push([y, x]);
208231
}
209232
}
210-
d = (d + 1) % 4;
233+
234+
i = (i + 1) % 4;
211235
}
236+
212237
return ans;
213238
};
214239
```

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

Lines changed: 47 additions & 22 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
@@ -180,31 +209,27 @@ func spiralMatrixIII(rows int, cols int, rStart int, cStart int) [][]int {
180209
* @return {number[][]}
181210
*/
182211
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
183-
const ans = [];
184-
const totalCells = rows * cols;
185-
const directions = [
186-
[0, 1],
187-
[1, 0],
188-
[0, -1],
189-
[-1, 0],
190-
];
191-
let step = 0;
192-
let d = 0;
193-
let [r, c] = [rStart, cStart];
194-
ans.push([r, c]);
195-
while (ans.length < totalCells) {
196-
if (d === 0 || d === 2) {
197-
step++;
198-
}
199-
for (let i = 0; i < step; i++) {
200-
r += directions[d][0];
201-
c += directions[d][1];
202-
if (r >= 0 && r < rows && c >= 0 && c < cols) {
203-
ans.push([r, c]);
212+
// prettier-ignore
213+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
214+
let [x, y, i, size] = [cStart, rStart, 0, 0];
215+
const ans = [[y, x]];
216+
const total = rows * cols;
217+
218+
while (ans.length < total) {
219+
if (i % 2 === 0) size++;
220+
221+
for (let j = 0; ans.length < total && j < size; j++) {
222+
x += dir[i][0];
223+
y += dir[i][1];
224+
225+
if (0 <= x && x < cols && 0 <= y && y < rows) {
226+
ans.push([y, x]);
204227
}
205228
}
206-
d = (d + 1) % 4;
229+
230+
i = (i + 1) % 4;
207231
}
232+
208233
return ans;
209234
};
210235
```

solution/0800-0899/0885.Spiral Matrix III/Solution.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,26 @@
66
* @return {number[][]}
77
*/
88
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
9-
const ans = [];
10-
const totalCells = rows * cols;
11-
const directions = [
12-
[0, 1],
13-
[1, 0],
14-
[0, -1],
15-
[-1, 0],
16-
];
17-
let step = 0;
18-
let d = 0;
19-
let [r, c] = [rStart, cStart];
20-
ans.push([r, c]);
21-
while (ans.length < totalCells) {
22-
if (d === 0 || d === 2) {
23-
step++;
24-
}
25-
for (let i = 0; i < step; i++) {
26-
r += directions[d][0];
27-
c += directions[d][1];
28-
if (r >= 0 && r < rows && c >= 0 && c < cols) {
29-
ans.push([r, c]);
9+
// prettier-ignore
10+
const dir = [[1,0],[0,1],[-1,0],[0,-1]]
11+
let [x, y, i, size] = [cStart, rStart, 0, 0];
12+
const ans = [[y, x]];
13+
const total = rows * cols;
14+
15+
while (ans.length < total) {
16+
if (i % 2 === 0) size++;
17+
18+
for (let j = 0; ans.length < total && j < size; j++) {
19+
x += dir[i][0];
20+
y += dir[i][1];
21+
22+
if (0 <= x && x < cols && 0 <= y && y < rows) {
23+
ans.push([y, x]);
3024
}
3125
}
32-
d = (d + 1) % 4;
26+
27+
i = (i + 1) % 4;
3328
}
29+
3430
return ans;
3531
};
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)