-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0885-spiral-matrix-iii.js
52 lines (48 loc) · 1.47 KB
/
0885-spiral-matrix-iii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 885. Spiral Matrix III
* https://leetcode.com/problems/spiral-matrix-iii/
* Difficulty: Medium
*
* You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner
* is at the first row and column in the grid, and the southeast corner is at the last row and
* column.
*
* You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you
* move outside the grid's boundary, we continue our walk outside the grid (but may return to the
* grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.
*
* Return an array of coordinates representing the positions of the grid in the order you visited
* them.
*/
/**
* @param {number} rows
* @param {number} cols
* @param {number} rStart
* @param {number} cStart
* @return {number[][]}
*/
var spiralMatrixIII = function(rows, cols, rStart, cStart) {
const result = [];
const total = rows * cols;
let row = rStart;
let col = cStart;
let step = 1;
let direction = 0;
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
result.push([row, col]);
while (result.length < total) {
for (let i = 0; i < 2; i++) {
const [dr, dc] = directions[direction];
for (let j = 0; j < step; j++) {
row += dr;
col += dc;
if (row >= 0 && row < rows && col >= 0 && col < cols) {
result.push([row, col]);
}
}
direction = (direction + 1) % 4;
}
step++;
}
return result;
};