Skip to content

Commit 18cd1ac

Browse files
authored
Merge pull request #2 from AE-Hertz/AE-Hertz-patch-solution-LC--0885
feat: add solutions to lc problem: No.0885
2 parents 07bc708 + 74c5827 commit 18cd1ac

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number} rows
3+
* @param {number} cols
4+
* @param {number} rStart
5+
* @param {number} cStart
6+
* @return {number[][]}
7+
*/
8+
var spiralMatrixIII = function(rows, cols, rStart, cStart) {
9+
let result = [];
10+
let totalCells = rows * cols;
11+
let directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
12+
let step = 0;
13+
let d = 0;
14+
let r = rStart, c = cStart;
15+
16+
result.push([r, c]);
17+
18+
while (result.length < totalCells) {
19+
if (d === 0 || d === 2) step++;
20+
21+
for (let i = 0; i < step; i++) {
22+
r += directions[d][0];
23+
c += directions[d][1];
24+
25+
if (r >= 0 && r < rows && c >= 0 && c < cols) {
26+
result.push([r, c]);
27+
}
28+
}
29+
30+
d = (d + 1) % 4;
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)