Skip to content

Commit 2f61b8c

Browse files
committed
feat: update js solution to lc problem: No.0885
1 parent 05a136f commit 2f61b8c

File tree

3 files changed

+54
-66
lines changed

3 files changed

+54
-66
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -213,31 +213,27 @@ function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: num
213213
* @return {number[][]}
214214
*/
215215
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
216-
const ans = [];
217-
const totalCells = rows * cols;
218-
const directions = [
219-
[0, 1],
220-
[1, 0],
221-
[0, -1],
222-
[-1, 0],
223-
];
224-
let step = 0;
225-
let d = 0;
226-
let [r, c] = [rStart, cStart];
227-
ans.push([r, c]);
228-
while (ans.length < totalCells) {
229-
if (d === 0 || d === 2) {
230-
step++;
231-
}
232-
for (let i = 0; i < step; i++) {
233-
r += directions[d][0];
234-
c += directions[d][1];
235-
if (r >= 0 && r < rows && c >= 0 && c < cols) {
236-
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]);
237231
}
238232
}
239-
d = (d + 1) % 4;
233+
234+
i = (i + 1) % 4;
240235
}
236+
241237
return ans;
242238
};
243239
```

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,27 @@ function spiralMatrixIII(rows: number, cols: number, rStart: number, cStart: num
209209
* @return {number[][]}
210210
*/
211211
var spiralMatrixIII = function (rows, cols, rStart, cStart) {
212-
const ans = [];
213-
const totalCells = rows * cols;
214-
const directions = [
215-
[0, 1],
216-
[1, 0],
217-
[0, -1],
218-
[-1, 0],
219-
];
220-
let step = 0;
221-
let d = 0;
222-
let [r, c] = [rStart, cStart];
223-
ans.push([r, c]);
224-
while (ans.length < totalCells) {
225-
if (d === 0 || d === 2) {
226-
step++;
227-
}
228-
for (let i = 0; i < step; i++) {
229-
r += directions[d][0];
230-
c += directions[d][1];
231-
if (r >= 0 && r < rows && c >= 0 && c < cols) {
232-
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]);
233227
}
234228
}
235-
d = (d + 1) % 4;
229+
230+
i = (i + 1) % 4;
236231
}
232+
237233
return ans;
238234
};
239235
```

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
};

0 commit comments

Comments
 (0)